﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalconDotNet;
using System.Drawing.Imaging;
using System.Drawing;

namespace AssemblyCYG
{
    public class HalconAlgorithm
    {
        public int innerRadius = 1246;
        public int outerRadius = 1280;
            
        public void FittingCircle(HObject ho_Image, HObject ho_ROI, out HObject ho_FitCircleContour,
    HTuple hv_cfgPara, out HTuple hv_circleRow, out HTuple hv_circleCol, out HTuple hv_circleRadius)
        {




            // Local iconic variables 

            HObject ho_ImageReduced, ho_Region, ho_RegionOpening;
            HObject ho_RegionDilation1, ho_RegionTrans, ho_Rectangle = null;
            HObject ho_RegionIntersection = null, ho_Contour = null;

            // Local control variables 

            HTuple hv_minGray = null, hv_maxGray = null;
            HTuple hv_contrast = null, hv_fitPNum = null, hv_sigma = null;
            HTuple hv_seekType = new HTuple(), hv_Width = null, hv_Height = null;
            HTuple hv_Row1 = null, hv_Column1 = null, hv_Radius = null;
            HTuple hv_rowList = null, hv_colList = null, hv_byNum = null;
            HTuple hv_i = null, hv_RowEnd = new HTuple(), hv_ColumnEnd = new HTuple();
            HTuple hv_RowCenter = new HTuple(), hv_ColCenter = new HTuple();
            HTuple hv_mRow = new HTuple(), hv_mColumn = new HTuple();
            HTuple hv_mPhi = new HTuple(), hv_mLength1 = new HTuple();
            HTuple hv_mLength2 = new HTuple(), hv_MaxLength = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_Distance = new HTuple(), hv_RowEdgeInt = new HTuple();
            HTuple hv_ColumnEdgeInt = new HTuple(), hv_Index = new HTuple();
            HTuple hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple();
            HTuple hv_PointOrder = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_FitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation1);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            hv_minGray = hv_cfgPara[0];
            hv_maxGray = hv_cfgPara[1];
            hv_contrast = hv_cfgPara[2];
            hv_fitPNum = hv_cfgPara[3];
            hv_sigma = hv_cfgPara[4];
            if ((int)(new HTuple(((hv_cfgPara.TupleSelect(5))).TupleEqual(0))) != 0)
            {
                //第一个点first，从内往外搜索
                hv_seekType = "first";
            }
            else
            {
                //从外往内搜索
                hv_seekType = "last";
            }
            hv_circleRow = 0;
            hv_circleCol = 0;
            hv_circleRadius = 0;
            ho_FitCircleContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_FitCircleContour);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI, out ho_ImageReduced);
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_minGray, hv_maxGray);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningCircle(ho_Region, out ho_RegionOpening, 1.5);
            ho_RegionDilation1.Dispose();
            HOperatorSet.DilationCircle(ho_RegionOpening, out ho_RegionDilation1, 5.5);
            ho_RegionTrans.Dispose();
            HOperatorSet.ShapeTrans(ho_RegionDilation1, out ho_RegionTrans, "outer_circle");
            //dilation_circle (RegionTrans, RegionDilation, 7)
            HOperatorSet.SmallestCircle(ho_RegionTrans, out hv_Row1, out hv_Column1, out hv_Radius);
            hv_rowList = new HTuple();
            hv_colList = new HTuple();
            HOperatorSet.TupleAbs(360 / hv_fitPNum, out hv_byNum);
            HTuple end_val27 = 360;
            HTuple step_val27 = hv_byNum;
            for (hv_i = 1; hv_i.Continue(end_val27, step_val27); hv_i = hv_i.TupleAdd(step_val27))
            {
                hv_RowEnd = hv_Row1 + ((((hv_i.TupleRad())).TupleSin()) * hv_Radius);
                hv_ColumnEnd = hv_Column1 + ((((hv_i.TupleRad())).TupleCos()) * hv_Radius);
                hv_RowCenter = (hv_RowEnd + hv_Row1) / 2;
                hv_ColCenter = (hv_ColumnEnd + hv_Column1) / 2;
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_RowCenter, hv_ColCenter, -(hv_i.TupleRad()
                    ), hv_Radius / 2, 5);
                ho_RegionIntersection.Dispose();
                HOperatorSet.Intersection(ho_Rectangle, ho_RegionDilation1, out ho_RegionIntersection
                    );
                HOperatorSet.SmallestRectangle2(ho_RegionIntersection, out hv_mRow, out hv_mColumn,
                    out hv_mPhi, out hv_mLength1, out hv_mLength2);
                if ((int)(new HTuple(hv_mLength1.TupleGreater(0))) != 0 && hv_mLength2 > 1)
                {
                    HOperatorSet.TupleMax2(hv_mLength1, hv_mLength2, out hv_MaxLength);
                    HOperatorSet.GenMeasureRectangle2(hv_mRow, hv_mColumn, -(hv_i.TupleRad()),
                        hv_MaxLength, 5, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                    //gen_rectangle2 (Rectangle1, mRow, mColumn, -rad(i), 10, 5)
                    HOperatorSet.MeasurePos(ho_ImageReduced, hv_MeasureHandle, 1, hv_contrast,
                        "all", hv_seekType, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude,
                        out hv_Distance);
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
                    if ((int)(new HTuple(hv_RowEdge.TupleGreater(1))) != 0)
                    {
                        HOperatorSet.TupleInt(hv_RowEdge, out hv_RowEdgeInt);
                        HOperatorSet.TupleInt(hv_ColumnEdge, out hv_ColumnEdgeInt);
                        HOperatorSet.GetRegionIndex(ho_ROI, hv_RowEdgeInt, hv_ColumnEdgeInt, out hv_Index);
                        //gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 8, 0.785398)
                        if ((int)(new HTuple(hv_Index.TupleGreaterEqual(0))) != 0)
                        {

                            HOperatorSet.TupleConcat(hv_rowList, hv_RowEdge, out hv_rowList);
                            HOperatorSet.TupleConcat(hv_colList, hv_ColumnEdge, out hv_colList);
                        }
                    }
                }

            }
            if ((int)(new HTuple((new HTuple(hv_rowList.TupleLength())).TupleGreater(2))) != 0)
            {
                ho_Contour.Dispose();

                HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_rowList, hv_colList);
                HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2, out hv_circleRow,
                    out hv_circleCol, out hv_circleRadius, out hv_StartPhi, out hv_EndPhi,
                    out hv_PointOrder);
                if ((int)(new HTuple(hv_circleRadius.TupleGreater(0))) != 0)
                {
                    ho_FitCircleContour.Dispose();
                    HOperatorSet.GenCircle(out ho_FitCircleContour, hv_circleRow, hv_circleCol,
                        hv_circleRadius);
                }
            }
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionDilation1.Dispose();
            ho_RegionTrans.Dispose();
            ho_Rectangle.Dispose();
            ho_RegionIntersection.Dispose();
            ho_Contour.Dispose();

            return;
        }
        public void FittingLine(HObject ho_Image, HObject ho_lineROI, out HObject ho_lineContour,
    HTuple hv_cfgPara, HTuple hv_LSJudge, HTuple hv_LSAngle, out HTuple hv_RowBegin,
    out HTuple hv_ColBegin, out HTuple hv_RowEnd, out HTuple hv_ColEnd)
        {

            // Local iconic variables 

            HObject ho_ImageReduced, ho_Rectangle = null;
            HObject ho_fitContour = null;

            // Local control variables 

            HTuple hv_centerRow = null, hv_centerCol = null;
            HTuple hv_oriPhi = null, hv_oriLength1 = null, hv_oriLength2 = null;
            HTuple hv_phi = null, hv_length1 = null, hv_length2 = null;
            HTuple hv_angleAbs = null, hv_sigma = null, hv_threshold = null;
            HTuple hv_transition = null, hv_select = null, hv_pointNum = null;
            HTuple hv_oneWidth = null, hv_chaCol = null, hv_chaRow = null;
            HTuple hv_sRow = null, hv_sCol = null, hv_Width = null;
            HTuple hv_Height = null, hv_RowUp = null, hv_ColUp = null;
            HTuple hv_index = null, hv_newRow = new HTuple(), hv_newCol = new HTuple();
            HTuple hv_Seconds1 = new HTuple(), hv_MeasureHandleTop = new HTuple();
            HTuple hv_RowEdge = new HTuple(), hv_ColumnEdge = new HTuple();
            HTuple hv_Amplitude1 = new HTuple(), hv_Distance1 = new HTuple();
            HTuple hv_Second2 = new HTuple(), hv_detectTime = new HTuple();
            HTuple hv_TopNr = new HTuple(), hv_TopNc = new HTuple();
            HTuple hv_TopDist = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_lineContour);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_fitContour);
            hv_RowBegin = new HTuple();
            hv_ColBegin = new HTuple();
            hv_RowEnd = new HTuple();
            hv_ColEnd = new HTuple();
            HOperatorSet.SmallestRectangle2(ho_lineROI, out hv_centerRow, out hv_centerCol,
                out hv_oriPhi, out hv_oriLength1, out hv_oriLength2);
            if (hv_centerRow.Length == 0 || hv_oriLength1.Length == 0) return;
            if (hv_oriLength1.D == 0) return;
            //中心点保持不变
            hv_phi = 0;
            hv_length1 = 0;
            hv_length2 = 0;
            if ((int)((new HTuple((new HTuple(hv_oriLength1.TupleGreater(hv_oriLength2))).TupleAnd(
                new HTuple(hv_LSJudge.TupleEqual(0))))).TupleOr((new HTuple(hv_oriLength2.TupleGreater(
                hv_oriLength1))).TupleAnd(new HTuple(hv_LSJudge.TupleEqual(1))))) != 0)
            {
                hv_phi = hv_oriPhi.Clone();
                hv_length1 = hv_oriLength1.Clone();
                hv_length2 = hv_oriLength2.Clone();
            }
            else
            {
                hv_phi = hv_oriPhi - 1.57;
                hv_length1 = hv_oriLength2.Clone();
                hv_length2 = hv_oriLength1.Clone();
            }

            HOperatorSet.TupleAbs(hv_phi - hv_LSAngle, out hv_angleAbs);
            if ((int)(new HTuple(hv_angleAbs.TupleGreater(2))) != 0)
            {
                hv_phi = hv_phi - 3.14159;
            }
            hv_sigma = hv_cfgPara[0];
            hv_threshold = hv_cfgPara[1];
            hv_transition = hv_cfgPara[2];
            hv_select = hv_cfgPara[3];
            hv_pointNum = hv_cfgPara[4];

            hv_oneWidth = (((hv_length2 * 2) / hv_pointNum)).TupleInt();
            hv_chaCol = (hv_phi.TupleSin()) * (hv_length2 - (hv_oneWidth / 2));
            hv_chaRow = (hv_phi.TupleCos()) * (hv_length2 - (hv_oneWidth / 2));
            hv_sRow = hv_centerRow - hv_chaRow;
            hv_sCol = hv_centerCol - hv_chaCol;
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_lineROI, out ho_ImageReduced);
            hv_RowUp = new HTuple();
            hv_ColUp = new HTuple();
            HTuple end_val34 = hv_pointNum - 1;
            HTuple step_val34 = 1;
            for (hv_index = 0; hv_index.Continue(end_val34, step_val34); hv_index = hv_index.TupleAdd(step_val34))
            {
                hv_newRow = hv_sRow + ((hv_oneWidth * (hv_phi.TupleCos())) * hv_index);
                hv_newCol = hv_sCol + ((hv_oneWidth * (hv_phi.TupleSin())) * hv_index);
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_newRow, hv_newCol, hv_phi,
                    hv_length1, hv_oneWidth / 2);
                HOperatorSet.CountSeconds(out hv_Seconds1);
                HOperatorSet.GenMeasureRectangle2(hv_newRow, hv_newCol, hv_phi, hv_length1,
                    hv_oneWidth / 2, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandleTop);
                HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandleTop, hv_sigma, hv_threshold,
                    hv_transition, hv_select, out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude1,
                    out hv_Distance1);
                HOperatorSet.CountSeconds(out hv_Second2);
                if ((int)(new HTuple(hv_RowEdge.TupleGreater(0))) != 0)
                {
                    HOperatorSet.TupleConcat(hv_RowUp, hv_RowEdge, out hv_RowUp);
                    HOperatorSet.TupleConcat(hv_ColUp, hv_ColumnEdge, out hv_ColUp);
                }
                HOperatorSet.CloseMeasure(hv_MeasureHandleTop);
                hv_detectTime = hv_Second2 - hv_Seconds1;
            }
            //如果拟合点的数量超过2
            if ((int)(new HTuple((new HTuple(hv_RowUp.TupleLength())).TupleGreater(2))) != 0)
            {
                ho_fitContour.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_fitContour, hv_RowUp, hv_ColUp);
                HOperatorSet.FitLineContourXld(ho_fitContour, "tukey", -1, 0, 5, 2, out hv_RowBegin,
                    out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_TopNr, out hv_TopNc,
                    out hv_TopDist);
                ho_lineContour.Dispose();
                HOperatorSet.GenRegionLine(out ho_lineContour, hv_RowBegin, hv_ColBegin, hv_RowEnd,
                    hv_ColEnd);
            }
            ho_ImageReduced.Dispose();
            ho_Rectangle.Dispose();
            ho_fitContour.Dispose();

            return;
        }
        public bool GetPuckPosition(HObject ho_Image, FitCirclePara locateFitCirclePara, FitLinePara locateFitLine1Para,
FitLinePara locateFitLine2Para, HObject ho_circleROI, HObject ho_Line1ROI, HObject ho_line2ROI, double outCircleRadius, double seekCornerStart, double seekCornerEnd, out HObject ho_fitCircleContour, out HObject ho_fitLine1Contour,
out HObject ho_fitLine2Contour, out HObject ho_fitAngleLine, out double circle_X, out double circle_Y, out double lineCorner_X, out double lineCorner_Y, 
            out double outAngle, out string bugInfo)
        {
            bugInfo = "";
            circle_X = 0;
            circle_Y = 0;
            lineCorner_X = 0;
            lineCorner_Y = 0;
            outAngle = 0;
            HOperatorSet.GenEmptyObj(out ho_fitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitLine1Contour);
            HOperatorSet.GenEmptyObj(out ho_fitLine2Contour);
            HOperatorSet.GenEmptyObj(out ho_fitAngleLine);
            try
            {
                HTuple hv_ImageWidth = null;
                HTuple hv_ImageHeight = null;
                HTuple hv_circle_Row = null;
                HTuple hv_circle_Col = null;
                HTuple hv_circle_Radius = null;
                HTuple hv_RowBeginLine1 = new HTuple(), hv_ColBeginLine1 = new HTuple();
                HTuple hv_RowEndLine1 = new HTuple(), hv_ColEndLine1 = new HTuple();
                HTuple hv_RowBeginLine2 = new HTuple(), hv_ColBeginLine2 = new HTuple();
                HTuple hv_RowEndLine2 = new HTuple(), hv_ColEndLine2 = new HTuple();
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高    
                //寻找定位圆....................................................
                ho_fitCircleContour.Dispose();
                HTuple hv_circlePara = new HTuple();
                hv_circlePara[0] = locateFitCirclePara.minGray;
                hv_circlePara[1] = locateFitCirclePara.maxGray;
                hv_circlePara[2] = locateFitCirclePara.contrast;
                hv_circlePara[3] = locateFitCirclePara.fitNum;
                hv_circlePara[4] = locateFitCirclePara.sigma;
                hv_circlePara[5] = locateFitCirclePara.seekPointPath;
                FittingCircle(ho_Image, ho_circleROI, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                                              
                if (hv_circle_Col <= 0)
                {
                    bugInfo = "圆2定位失败！";
                    return false;
                }
                //寻找线1....................................................
                HTuple hv_Line1Para = new HTuple();
                hv_Line1Para[0] = locateFitLine1Para.sigma;
                hv_Line1Para[1] = locateFitLine1Para.contrast;
                switch (locateFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_Line1Para[2] = "all";
                        break;
                    case 1:
                        hv_Line1Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line1Para[2] = "negative";
                        break;
                }
                switch (locateFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_Line1Para[3] = "all";
                        break;
                    case 1:
                        hv_Line1Para[3] = "first";
                        break;
                    case 2:
                        hv_Line1Para[3] = "last";
                        break;
                }
                hv_Line1Para[4] = locateFitLine1Para.fitNum;
                ho_fitLine1Contour.Dispose();
                FittingLine(ho_Image, ho_Line1ROI, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                    locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);
                //寻找线2....................................................
                HTuple hv_Line2Para = new HTuple();
                hv_Line2Para[0] = locateFitLine2Para.sigma;
                hv_Line2Para[1] = locateFitLine2Para.contrast;
                switch (locateFitLine2Para.grayTransition)
                {
                    case 0:
                        hv_Line2Para[2] = "all";
                        break;
                    case 1:
                        hv_Line2Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line2Para[2] = "negative";
                        break;
                }
                switch (locateFitLine2Para.pointPosition)
                {
                    case 0:
                        hv_Line2Para[3] = "all";
                        break;
                    case 1:
                        hv_Line2Para[3] = "first";
                        break;
                    case 2:
                        hv_Line2Para[3] = "last";
                        break;
                }
                hv_Line2Para[4] = locateFitLine2Para.fitNum;
                ho_fitLine2Contour.Dispose();
                FittingLine(ho_Image, ho_line2ROI, out ho_fitLine2Contour, hv_Line2Para, locateFitLine2Para.LSJudge,
                     locateFitLine2Para.LSAngle, out hv_RowBeginLine2, out hv_ColBeginLine2, out hv_RowEndLine2, out hv_ColEndLine2);
                //寻找两线和圆之间的交点....................................................
                HTuple corner1_row = null;
                HTuple corner1_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine1, hv_ColBeginLine1, hv_RowEndLine1, hv_ColEndLine1, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner1_row, out corner1_col);
                HTuple corner2_row = null;
                HTuple corner2_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine2, hv_ColBeginLine2, hv_RowEndLine2, hv_ColEndLine2, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner2_row, out corner2_col);
                HTuple cornerCenter_row = (corner2_row + corner1_row) / 2;
                HTuple cornerCenter_col = (corner2_col + corner1_col) / 2;
                if (cornerCenter_row.Length == 0)
                {
                    bugInfo = "交点提取失败";
                    return false;
                }

                if(true)
                {
                    HObject hv_cross = null;
                    HOperatorSet.GenEmptyObj(out hv_cross);
                    hv_cross.Dispose();
                    HTuple hv_result = null;
                    Fit8PointsCircle_puck(ho_Image, out hv_cross, hv_circle_Row, hv_circle_Col, hv_circle_Radius, cornerCenter_row, cornerCenter_col, out hv_circle_Row,
                        out hv_circle_Col, out  hv_circle_Radius, out hv_result);
                    if (hv_result == 1)
                    {
                        bugInfo = "交点提取失败!";
                        return false;
                    }
                    hv_cross.Dispose();
                }
               
                //求交点和圆心连线的角度.................................................
  
                HTuple hv_angle = null;

                //if (cornerCenter_row[0] < hv_circle_Row)
                    //HOperatorSet.AngleLl(cornerCenter_row[0], cornerCenter_col[0], hv_circle_Row, hv_circle_Col, 0, 0, 0, hv_ImageWidth, out hv_angle);
                //else
                HOperatorSet.AngleLl(hv_circle_Row, hv_circle_Col, cornerCenter_row[0], cornerCenter_col[0], 0, 0, 0, hv_ImageWidth, out hv_angle);

                
                ho_fitAngleLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_fitAngleLine, cornerCenter_row[0], cornerCenter_col[0], hv_circle_Row, hv_circle_Col);
                //ho_Image.Dispose();
                circle_X = hv_circle_Col.D;
                circle_Y = hv_circle_Row.D;
                lineCorner_X = cornerCenter_col[0].D;
                lineCorner_Y = cornerCenter_row[0].D;
                outAngle = hv_angle.D;
                return true;
            }
            catch (Exception ex)
            {
                //ho_Image.Dispose();
                bugInfo = ex.Message;
                return false;
            }
        }
        public bool GetPuckPositionMatch(HObject ho_Image, HTuple hv_matchModel, MatchPara locateMatchPara, FitCirclePara locateFitCirclePara, FitLinePara locateFitLine1Para,
FitLinePara locateFitLine2Para, HObject ho_circleROI, HObject ho_line1ROI, HObject ho_line2ROI, double outCircleRadius, double seekCornerStart, double seekCornerEnd, out HObject ho_fitCircleContour, out HObject ho_fitLine1Contour,
out HObject ho_fitLine2Contour, out HObject ho_fitAngleLine, out double circle_X, out double circle_Y, out double lineCorner_X, out double lineCorner_Y,
    out double outAngle, out string bugInfo)
        {
            bugInfo = "";
            circle_X = 0;
            circle_Y = 0;
            lineCorner_X = 0;
            lineCorner_Y = 0;
            outAngle = 0;
            HOperatorSet.GenEmptyObj(out ho_fitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitLine1Contour);
            HOperatorSet.GenEmptyObj(out ho_fitLine2Contour);
            HOperatorSet.GenEmptyObj(out ho_fitAngleLine);
            try
            {
                HTuple hv_ImageWidth = null;
                HTuple hv_ImageHeight = null;
                HTuple hv_circle_Row = null;
                HTuple hv_circle_Col = null;
                HTuple hv_circle_Radius = null;
                HTuple hv_RowBeginLine1 = new HTuple(), hv_ColBeginLine1 = new HTuple();
                HTuple hv_RowEndLine1 = new HTuple(), hv_ColEndLine1 = new HTuple();
                HTuple hv_RowBeginLine2 = new HTuple(), hv_ColBeginLine2 = new HTuple();
                HTuple hv_RowEndLine2 = new HTuple(), hv_ColEndLine2 = new HTuple();
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高    
                //模板匹配.........................................................
                int seekAngleStart = locateMatchPara.seekAngleStart;
                int seekAngleRange = locateMatchPara.seekAngleRange;
                int seekNum = locateMatchPara.seekModelNum;
                int seekScore = locateMatchPara.seekModelScore;
                HTuple hv_modelRow = new HTuple();
                HTuple hv_modelCol = new HTuple();
                HTuple hv_modelAngle = new HTuple();
                HTuple hv_modelScore = new HTuple();
                if (hv_matchModel == null)
                {
                    bugInfo = "模板为空！";
                    return false;
                }
                if (locateMatchPara.modelMatchType == 0)  //基于形状
                {
                    HOperatorSet.FindShapeModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01,
                            seekNum, 0.5, "least_squares", 0, 0.9, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore); //查找模板  
                }
                else if (locateMatchPara.modelMatchType == 1)  //基于灰度
                {
                    HOperatorSet.FindNccModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01, seekNum, 0.5, "true",
                        0, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore);
                }
                if (hv_modelScore.Length != 1)
                {
                    bugInfo = "模板定位失败！";
                    return false;
                }
                //建立放射矩阵，并求除映射区域
                HTuple hv_HomMat2D;
                HOperatorSet.VectorAngleToRigid(locateMatchPara.modelLocationRow, locateMatchPara.modelLocationCol, 0, hv_modelRow[0],
                       hv_modelCol[0], hv_modelAngle[0], out hv_HomMat2D);   //根据模板坐标，求出放射矩阵
                HObject ho_AffinTransFitCircle = null;
                HObject ho_AffinTransFitLine1 = null;
                HObject ho_AffinTransFitLine2 = null;
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitCircle);
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitLine1);
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitLine2);
                ho_AffinTransFitCircle.Dispose();
                HOperatorSet.AffineTransRegion(ho_circleROI, out ho_AffinTransFitCircle, hv_HomMat2D, "nearest_neighbor");
                ho_AffinTransFitLine1.Dispose();
                HOperatorSet.AffineTransRegion(ho_line1ROI, out ho_AffinTransFitLine1, hv_HomMat2D, "nearest_neighbor");
                ho_AffinTransFitLine2.Dispose();
                HOperatorSet.AffineTransRegion(ho_line2ROI, out ho_AffinTransFitLine2, hv_HomMat2D, "nearest_neighbor");
                //寻找定位圆....................................................
                ho_fitCircleContour.Dispose();
                HTuple hv_circlePara = new HTuple();
                hv_circlePara[0] = locateFitCirclePara.minGray;
                hv_circlePara[1] = locateFitCirclePara.maxGray;
                hv_circlePara[2] = locateFitCirclePara.contrast;
                hv_circlePara[3] = locateFitCirclePara.fitNum;
                hv_circlePara[4] = locateFitCirclePara.sigma;
                hv_circlePara[5] = locateFitCirclePara.seekPointPath;
                if (locateFitCirclePara.isInheritModel)
                    FittingCircle(ho_Image, ho_AffinTransFitCircle, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                else
                    FittingCircle(ho_Image, ho_circleROI, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                if (hv_circle_Col <= 0)
                {
                    bugInfo = "圆2定位失败！";
                    return false;
                }
                //寻找线1....................................................
                HTuple hv_Line1Para = new HTuple();
                hv_Line1Para[0] = locateFitLine1Para.sigma;
                hv_Line1Para[1] = locateFitLine1Para.contrast;
                switch (locateFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_Line1Para[2] = "all";
                        break;
                    case 1:
                        hv_Line1Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line1Para[2] = "negative";
                        break;
                }
                switch (locateFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_Line1Para[3] = "all";
                        break;
                    case 1:
                        hv_Line1Para[3] = "first";
                        break;
                    case 2:
                        hv_Line1Para[3] = "last";
                        break;
                }
                hv_Line1Para[4] = locateFitLine1Para.fitNum;
                ho_fitLine1Contour.Dispose();
                FittingLine(ho_Image, ho_line1ROI, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                    locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);
                //寻找线2....................................................
                HTuple hv_Line2Para = new HTuple();
                hv_Line2Para[0] = locateFitLine2Para.sigma;
                hv_Line2Para[1] = locateFitLine2Para.contrast;
                switch (locateFitLine2Para.grayTransition)
                {
                    case 0:
                        hv_Line2Para[2] = "all";
                        break;
                    case 1:
                        hv_Line2Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line2Para[2] = "negative";
                        break;
                }
                switch (locateFitLine2Para.pointPosition)
                {
                    case 0:
                        hv_Line2Para[3] = "all";
                        break;
                    case 1:
                        hv_Line2Para[3] = "first";
                        break;
                    case 2:
                        hv_Line2Para[3] = "last";
                        break;
                }
                hv_Line2Para[4] = locateFitLine2Para.fitNum;
                ho_fitLine2Contour.Dispose();
                FittingLine(ho_Image, ho_line2ROI, out ho_fitLine2Contour, hv_Line2Para, locateFitLine2Para.LSJudge,
                     locateFitLine2Para.LSAngle, out hv_RowBeginLine2, out hv_ColBeginLine2, out hv_RowEndLine2, out hv_ColEndLine2);
                //寻找两线和圆之间的交点....................................................
                HTuple corner1_row = null;
                HTuple corner1_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine1, hv_ColBeginLine1, hv_RowEndLine1, hv_ColEndLine1, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner1_row, out corner1_col);
                HTuple corner2_row = null;
                HTuple corner2_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine2, hv_ColBeginLine2, hv_RowEndLine2, hv_ColEndLine2, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner2_row, out corner2_col);
                //求交点和圆心连线的调度.................................................
                HTuple cornerCenter_row = (corner2_row + corner1_row) / 2;
                HTuple cornerCenter_col = (corner2_col + corner1_col) / 2;
                if (cornerCenter_row.Length == 0)
                {
                    bugInfo = "交点提取失败";
                    return false;
                }
                HTuple hv_angle = null;

                //if (cornerCenter_row[0] < hv_circle_Row)
                //HOperatorSet.AngleLl(cornerCenter_row[0], cornerCenter_col[0], hv_circle_Row, hv_circle_Col, 0, 0, 0, hv_ImageWidth, out hv_angle);
                //else
                HOperatorSet.AngleLl(hv_circle_Row, hv_circle_Col, cornerCenter_row[0], cornerCenter_col[0], 0, 0, 0, hv_ImageWidth, out hv_angle);


                ho_fitAngleLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_fitAngleLine, cornerCenter_row[0], cornerCenter_col[0], hv_circle_Row, hv_circle_Col);
                //ho_Image.Dispose();
                circle_X = hv_circle_Col.D;
                circle_Y = hv_circle_Row.D;
                lineCorner_X = cornerCenter_col[0].D;
                lineCorner_Y = cornerCenter_row[0].D;
                outAngle = hv_angle.D;
                return true;
            }
            catch (Exception ex)
            {
                //ho_Image.Dispose();
                bugInfo = ex.Message;
                return false;
            }
        }
        public bool GetSpringPosition_match(HObject ho_Image, HTuple hv_matchModel, MatchPara locateMatchPara, FitCirclePara locateFitCirclePara, FitLinePara locateFitLine1Para,
    FitLinePara locateFitLine2Para, HObject ho_circleROI, HObject ho_line1ROI, HObject ho_line2ROI, double outCircleRadius, double seekCornerStart, double seekCornerEnd, out HObject ho_fitCircleContour, out HObject ho_fitLine1Contour,
    out HObject ho_fitLine2Contour, out HObject ho_fitAngleLine, out double circle_X, out double circle_Y, out double outAngle, out string bugInfo)
        {
            bugInfo = "";
            circle_X = 0;
            circle_Y = 0;
            outAngle = 0;
            HOperatorSet.GenEmptyObj(out ho_fitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitLine1Contour);
            HOperatorSet.GenEmptyObj(out ho_fitLine2Contour);
            HOperatorSet.GenEmptyObj(out ho_fitAngleLine);
            try
            {
                HTuple hv_ImageWidth = null;
                HTuple hv_ImageHeight = null;
                HTuple hv_circle_Row = null;
                HTuple hv_circle_Col = null;
                HTuple hv_circle_Radius = null;
                HTuple hv_RowBeginLine1 = new HTuple(), hv_ColBeginLine1 = new HTuple();
                HTuple hv_RowEndLine1 = new HTuple(), hv_ColEndLine1 = new HTuple();
                HTuple hv_RowBeginLine2 = new HTuple(), hv_ColBeginLine2 = new HTuple();
                HTuple hv_RowEndLine2 = new HTuple(), hv_ColEndLine2 = new HTuple();
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高    
                //模板匹配.........................................................
                int seekAngleStart = locateMatchPara.seekAngleStart;
                int seekAngleRange = locateMatchPara.seekAngleRange;
                int seekNum = locateMatchPara.seekModelNum;
                int seekScore = locateMatchPara.seekModelScore;
                HTuple hv_modelRow = new HTuple();
                HTuple hv_modelCol = new HTuple();
                HTuple hv_modelAngle = new HTuple();
                HTuple hv_modelScore = new HTuple();
                if (hv_matchModel == null)
                {
                    bugInfo = "模板为空！";
                    return false;
                }
                if (locateMatchPara.modelMatchType == 0)  //基于形状
                {
                    HOperatorSet.FindShapeModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01,
                            seekNum, 0.5, "least_squares", 0, 0.9, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore); //查找模板  
                }
                else if (locateMatchPara.modelMatchType == 1)  //基于灰度
                {
                    HOperatorSet.FindNccModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01, seekNum, 0.5, "true",
                        0, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore);
                }
                if (hv_modelScore.Length != 1)
                {
                    bugInfo = "模板定位失败！";
                    return false;
                }
                //建立放射矩阵，并求除映射区域
                HTuple hv_HomMat2D;
                HOperatorSet.VectorAngleToRigid(locateMatchPara.modelLocationRow, locateMatchPara.modelLocationCol, 0, hv_modelRow[0],
                       hv_modelCol[0], hv_modelAngle[0], out hv_HomMat2D);   //根据模板坐标，求出放射矩阵
                HObject ho_AffinTransFitCircle = null;
                HObject ho_AffinTransFitLine1 = null;
                HObject ho_AffinTransFitLine2 = null;
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitCircle);
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitLine1);
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitLine2);
                ho_AffinTransFitCircle.Dispose();
                HOperatorSet.AffineTransRegion(ho_circleROI, out ho_AffinTransFitCircle, hv_HomMat2D, "nearest_neighbor");
                ho_AffinTransFitLine1.Dispose();
                HOperatorSet.AffineTransRegion(ho_line1ROI, out ho_AffinTransFitLine1, hv_HomMat2D, "nearest_neighbor");
                ho_AffinTransFitLine2.Dispose();
                HOperatorSet.AffineTransRegion(ho_line2ROI, out ho_AffinTransFitLine2, hv_HomMat2D, "nearest_neighbor");
                //寻找定位圆....................................................
                ho_fitCircleContour.Dispose();
                HTuple hv_circlePara = new HTuple();
                hv_circlePara[0] = locateFitCirclePara.minGray;
                hv_circlePara[1] = locateFitCirclePara.maxGray;
                hv_circlePara[2] = locateFitCirclePara.contrast;
                hv_circlePara[3] = locateFitCirclePara.fitNum;
                hv_circlePara[4] = locateFitCirclePara.sigma;
                hv_circlePara[5] = locateFitCirclePara.seekPointPath;
                if (locateFitCirclePara.isInheritModel)
                    FittingCircle(ho_Image, ho_AffinTransFitCircle, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                else
                    FittingCircle(ho_Image, ho_circleROI, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                if (hv_circle_Col <= 0)
                {
                    bugInfo = "圆2定位失败！";
                    return false;
                }
                //寻找线1....................................................
                HTuple hv_Line1Para = new HTuple();
                hv_Line1Para[0] = locateFitLine1Para.sigma;
                hv_Line1Para[1] = locateFitLine1Para.contrast;
                switch (locateFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_Line1Para[2] = "all";
                        break;
                    case 1:
                        hv_Line1Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line1Para[2] = "negative";
                        break;
                }
                switch (locateFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_Line1Para[3] = "all";
                        break;
                    case 1:
                        hv_Line1Para[3] = "first";
                        break;
                    case 2:
                        hv_Line1Para[3] = "last";
                        break;
                }
                hv_Line1Para[4] = locateFitLine1Para.fitNum;
                ho_fitLine1Contour.Dispose();
                if (locateFitLine1Para.isInheritModel)
                    FittingLine(ho_Image, ho_AffinTransFitLine1, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                        locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);
                else
                    FittingLine(ho_Image, ho_line1ROI, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                        locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);
                //寻找线2....................................................
                HTuple hv_Line2Para = new HTuple();
                hv_Line2Para[0] = locateFitLine2Para.sigma;
                hv_Line2Para[1] = locateFitLine2Para.contrast;
                switch (locateFitLine2Para.grayTransition)
                {
                    case 0:
                        hv_Line2Para[2] = "all";
                        break;
                    case 1:
                        hv_Line2Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line2Para[2] = "negative";
                        break;
                }
                switch (locateFitLine2Para.pointPosition)
                {
                    case 0:
                        hv_Line2Para[3] = "all";
                        break;
                    case 1:
                        hv_Line2Para[3] = "first";
                        break;
                    case 2:
                        hv_Line2Para[3] = "last";
                        break;
                }
                hv_Line2Para[4] = locateFitLine2Para.fitNum;
                ho_fitLine2Contour.Dispose();
                if (locateFitLine2Para.isInheritModel)
                    FittingLine(ho_Image, ho_AffinTransFitLine2, out ho_fitLine2Contour, hv_Line2Para, locateFitLine2Para.LSJudge,
                        locateFitLine2Para.LSAngle, out hv_RowBeginLine2, out hv_ColBeginLine2, out hv_RowEndLine2, out hv_ColEndLine2);
                else
                    FittingLine(ho_Image, ho_line2ROI, out ho_fitLine2Contour, hv_Line2Para, locateFitLine2Para.LSJudge,
                        locateFitLine2Para.LSAngle, out hv_RowBeginLine2, out hv_ColBeginLine2, out hv_RowEndLine2, out hv_ColEndLine2);
                //寻找两线和圆之间的交点....................................................
                HTuple corner1_row = null;
                HTuple corner1_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine1, hv_ColBeginLine1, hv_RowEndLine1, hv_ColEndLine1, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner1_row, out corner1_col);
                HTuple corner2_row = null;
                HTuple corner2_col = null;
                HOperatorSet.IntersectionLineCircle(hv_RowBeginLine2, hv_ColBeginLine2, hv_RowEndLine2, hv_ColEndLine2, hv_circle_Row,
                    hv_circle_Col, outCircleRadius, seekCornerStart, seekCornerEnd, "positive", out corner2_row, out corner2_col);
                //求交点和圆心连线的调度.................................................
                HTuple cornerCenter_row = (corner2_row + corner1_row) / 2;
                HTuple cornerCenter_col = (corner2_col + corner1_col) / 2;
                if (cornerCenter_row.Length == 0)
                {
                    bugInfo = "交点提取失败";
                    return false;
                }
                //HTuple cornerCenter_min = null;
                HTuple hv_findIndex = 0;
                //if (cornerType == 0)   //求行坐标的最小值
                //{
                //    HOperatorSet.TupleMin(cornerCenter_row, out cornerCenter_min);
                //    HOperatorSet.TupleFind(cornerCenter_row, cornerCenter_min, out hv_findIndex);
                //}
                //else if (cornerType == 1)   //求列坐标的最小值
                //{
                //    HOperatorSet.TupleMin(cornerCenter_col, out cornerCenter_min);
                //    HOperatorSet.TupleFind(cornerCenter_col, cornerCenter_min, out hv_findIndex);
                //}
                HTuple hv_angle = null;
                HOperatorSet.AngleLl(cornerCenter_row[hv_findIndex], cornerCenter_col[hv_findIndex], hv_circle_Row, hv_circle_Col, 0, 0, 0, hv_ImageWidth, out hv_angle);
                ho_fitAngleLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_fitAngleLine, cornerCenter_row[hv_findIndex], cornerCenter_col[hv_findIndex], hv_circle_Row, hv_circle_Col);
                //ho_Image.Dispose();
                circle_X = hv_circle_Col.D;
                circle_Y = hv_circle_Row.D;
                outAngle = hv_angle.D;
                ho_AffinTransFitCircle.Dispose();
                ho_AffinTransFitLine1.Dispose();
                ho_AffinTransFitLine2.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                //ho_Image.Dispose();
                bugInfo = ex.Message;
                return false;
            }
        }
        public bool GetCoilPosition_match(HObject ho_Image, HTuple hv_matchModel, MatchPara locateMatchPara, FitCirclePara locateFitCirclePara, FitLinePara locateFitLine1Para,
            HObject ho_cicleROI, HObject ho_lineROI, out HObject ho_fitCircleContour, out HObject ho_fitLine1Contour, out double circle_X, 
            out double circle_Y, out double outAngle, out string bugInfo)
        {
            bugInfo = "";
            circle_X = 0;
            circle_Y = 0;
            outAngle = 0;
            HOperatorSet.GenEmptyObj(out ho_fitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitLine1Contour);
            try
            {
                HTuple hv_ImageWidth = null;
                HTuple hv_ImageHeight = null;
                HTuple hv_circle_Row = null;
                HTuple hv_circle_Col = null;
                HTuple hv_circle_Radius = null;
                HTuple hv_RowBeginLine1 = new HTuple(), hv_ColBeginLine1 = new HTuple();
                HTuple hv_RowEndLine1 = new HTuple(), hv_ColEndLine1 = new HTuple();
                HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);  //获取图像的宽和高    
                //模板匹配.........................................................
                int seekAngleStart = locateMatchPara.seekAngleStart;
                int seekAngleRange = locateMatchPara.seekAngleRange;
                int seekNum = locateMatchPara.seekModelNum;
                int seekScore = locateMatchPara.seekModelScore;
                HTuple hv_modelRow = new HTuple();
                HTuple hv_modelCol = new HTuple();
                HTuple hv_modelAngle = new HTuple();
                HTuple hv_modelScore = new HTuple();
                if (hv_matchModel == null)
                {
                    bugInfo = "模板为空！";
                    return false;
                }
                if (locateMatchPara.modelMatchType == 0)  //基于形状
                {
                    HOperatorSet.FindShapeModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01,
                            seekNum, 0.5, "least_squares", 0, 0.9, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore); //查找模板  
                }               
                else if (locateMatchPara.modelMatchType == 1)  //基于灰度
                {
                    HOperatorSet.FindNccModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01, seekNum, 0.5, "true",
                        0, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore);
                }
                
                if (hv_modelScore.Length != 1)
                {
                    bugInfo = "模板定位失败！";
                    return false;
                }
                //建立放射矩阵，并求除映射区域
                HTuple hv_HomMat2D;
                HOperatorSet.VectorAngleToRigid(locateMatchPara.modelLocationRow, locateMatchPara.modelLocationCol, 0, hv_modelRow[0],
                       hv_modelCol[0], hv_modelAngle[0], out hv_HomMat2D);   //根据模板坐标，求出放射矩阵
                HObject ho_AffinTransFitCircle = null;
                HObject ho_AffinTransFitLine1 = null;
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitCircle);
                HOperatorSet.GenEmptyObj(out ho_AffinTransFitLine1);
                ho_AffinTransFitCircle.Dispose();
                HOperatorSet.AffineTransRegion(ho_cicleROI, out ho_AffinTransFitCircle, hv_HomMat2D, "nearest_neighbor");
                ho_AffinTransFitLine1.Dispose();
                HOperatorSet.AffineTransRegion(ho_lineROI, out ho_AffinTransFitLine1, hv_HomMat2D, "nearest_neighbor");
                //寻找定位圆....................................................
                ho_fitCircleContour.Dispose();
                HTuple hv_circlePara = new HTuple();
                hv_circlePara[0] = locateFitCirclePara.minGray;
                hv_circlePara[1] = locateFitCirclePara.maxGray;
                hv_circlePara[2] = locateFitCirclePara.contrast;
                hv_circlePara[3] = locateFitCirclePara.fitNum;
                hv_circlePara[4] = locateFitCirclePara.sigma;
                hv_circlePara[5] = locateFitCirclePara.seekPointPath;
                if (locateFitCirclePara.isInheritModel)
                    FittingCircle(ho_Image, ho_AffinTransFitCircle, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                else
                    FittingCircle(ho_Image, ho_cicleROI, out ho_fitCircleContour, hv_circlePara, out hv_circle_Row, out hv_circle_Col, out hv_circle_Radius);
                if (hv_circle_Col <= 0)
                {
                    bugInfo = "圆2定位失败！";
                    return false;
                }
                //寻找线1....................................................
                HTuple hv_Line1Para = new HTuple();
                hv_Line1Para[0] = locateFitLine1Para.sigma;
                hv_Line1Para[1] = locateFitLine1Para.contrast;
                switch (locateFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_Line1Para[2] = "all";
                        break;
                    case 1:
                        hv_Line1Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line1Para[2] = "negative";
                        break;
                }
                switch (locateFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_Line1Para[3] = "all";
                        break;
                    case 1:
                        hv_Line1Para[3] = "first";
                        break;
                    case 2:
                        hv_Line1Para[3] = "last";
                        break;
                }
                hv_Line1Para[4] = locateFitLine1Para.fitNum;
                ho_fitLine1Contour.Dispose();
                if (locateFitLine1Para.isInheritModel)
                    FittingLine(ho_Image, ho_AffinTransFitLine1, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                        locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);
                else
                    FittingLine(ho_Image, ho_lineROI, out ho_fitLine1Contour, hv_Line1Para, locateFitLine1Para.LSJudge,
                        locateFitLine1Para.LSAngle, out hv_RowBeginLine1, out hv_ColBeginLine1, out hv_RowEndLine1, out hv_ColEndLine1);

                HTuple hv_angle = null;

                if (hv_ColBeginLine1 < hv_ColEndLine1)
                    HOperatorSet.AngleLl(hv_RowBeginLine1, hv_ColBeginLine1, hv_RowEndLine1, hv_ColEndLine1, 0, 0, 0, hv_ImageWidth, out hv_angle);
                else
                    HOperatorSet.AngleLl(hv_RowEndLine1, hv_ColEndLine1, hv_RowBeginLine1, hv_ColBeginLine1, 0, 0, 0, hv_ImageWidth, out hv_angle);

                //ho_Image.Dispose();
                if (true)
                {
                    HObject hv_cross = null;
                    HOperatorSet.GenEmptyObj(out hv_cross);
                    hv_cross.Dispose();
                    HTuple hv_result = null;
                    HTuple cornerCenter_row, cornerCenter_col = null;
                    cornerCenter_row = (hv_RowBeginLine1 + hv_RowEndLine1) / 2;
                    cornerCenter_col = (hv_ColBeginLine1 + hv_ColEndLine1) / 2;
                    Fit8PointsCircle_coil(ho_Image, out hv_cross, hv_circle_Row, hv_circle_Col, hv_circle_Radius, cornerCenter_row, cornerCenter_col, out hv_circle_Row,
                        out hv_circle_Col, out  hv_circle_Radius, out hv_result);
                    if (hv_result == 1)
                    {
                        bugInfo = "交点提取失败!";
                        return false;
                    }
                    hv_cross.Dispose();
                }
                circle_X = hv_circle_Col.D;
                circle_Y = hv_circle_Row.D;
                outAngle = hv_angle.D;
                ho_AffinTransFitCircle.Dispose();
                ho_AffinTransFitLine1.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                //ho_Image.Dispose();
                bugInfo = ex.Message;
                return false;
            }
        }
        public void Fit8PointsCircle_coil(HObject ho_Image, out HObject ho_Cross, HTuple hv_circleInitRow,
    HTuple hv_circleInitCol, HTuple hv_circleInitRadius, HTuple hv_cornerRow, HTuple hv_cornerCol,
    out HTuple hv_newCircleRow, out HTuple hv_newCircleCol, out HTuple hv_newCircleRadius,
    out HTuple hv_result)
        {




            // Local iconic variables 

            HObject ho_RegionLines, ho_Rectangle = null;
            HObject ho_Cross1 = null, ho_Cross2 = null, ho_Contour = null;
            HObject ho_Circle1 = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_cornerRowTuple = new HTuple(), hv_cornerColTuple = new HTuple();
            HTuple hv_cornerRowTuple1 = new HTuple(), hv_cornerColTuple1 = new HTuple();
            HTuple hv_corner8Row = new HTuple(), hv_corner8Col = new HTuple();
            HTuple hv_index = new HTuple(), hv_rotate = new HTuple();
            HTuple hv_isFindPoint = new HTuple(), hv_i = new HTuple();
            HTuple hv_newRotate = new HTuple(), hv_HomMat2DIdentity = new HTuple();
            HTuple hv_HomMat2DRotate = new HTuple(), hv_newcornerRow = new HTuple();
            HTuple hv_newcornerCol = new HTuple(), hv_seekStart = new HTuple();
            HTuple hv_seekEnd = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_MeasureHandle = new HTuple();
            HTuple hv_RowEdge = new HTuple(), hv_ColumnEdge = new HTuple();
            HTuple hv_Amplitude = new HTuple(), hv_Distance = new HTuple();
            HTuple hv_seekStart1 = new HTuple(), hv_seekEnd1 = new HTuple();
            HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_RowEdge1 = new HTuple(), hv_ColumnEdge1 = new HTuple();
            HTuple hv_Amplitude1 = new HTuple(), hv_Distance1 = new HTuple();
            HTuple hv_Number = new HTuple(), hv_StartPhi = new HTuple();
            HTuple hv_EndPhi = new HTuple(), hv_PointOrder = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Cross1);
            HOperatorSet.GenEmptyObj(out ho_Cross2);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            hv_newCircleRow = new HTuple();
            hv_newCircleCol = new HTuple();
            hv_newCircleRadius = new HTuple();
            hv_result = new HTuple();
            hv_result.Dispose();
            hv_result = 0;
            hv_newCircleRow.Dispose();
            hv_newCircleRow = 0;
            hv_newCircleRadius.Dispose();
            hv_newCircleRadius = 0;
            hv_newCircleCol.Dispose();
            hv_newCircleCol = 0;
            //gen_circle (Circle, circleInitRow, circleInitCol, circleInitRadius)
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_RegionLines.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines, hv_circleInitRow, hv_circleInitCol,
                hv_cornerRow, hv_cornerCol);
            hv_cornerRowTuple.Dispose();
            hv_cornerRowTuple = new HTuple();
            hv_cornerColTuple.Dispose();
            hv_cornerColTuple = new HTuple();
            hv_cornerRowTuple1.Dispose();
            hv_cornerRowTuple1 = new HTuple();
            hv_cornerColTuple1.Dispose();
            hv_cornerColTuple1 = new HTuple();
            hv_corner8Row.Dispose();
            hv_corner8Row = new HTuple();
            hv_corner8Col.Dispose();
            hv_corner8Col = new HTuple();
            for (hv_index = 1; (int)hv_index <= 4; hv_index = (int)hv_index + 1)
            {
                hv_rotate.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_rotate = 45 * hv_index;
                }
                hv_isFindPoint.Dispose();
                hv_isFindPoint = 0;
                for (hv_i = -5; (int)hv_i <= 5; hv_i = (int)hv_i + 1)
                {
                    hv_newRotate.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_newRotate = hv_rotate + (hv_i * 0.2);
                    }
                    hv_HomMat2DIdentity.Dispose();
                    HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_HomMat2DRotate.Dispose();
                        HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, hv_newRotate.TupleRad(),
                            hv_circleInitRow, hv_circleInitCol, out hv_HomMat2DRotate);
                    }
                    hv_newcornerRow.Dispose(); hv_newcornerCol.Dispose();
                    HOperatorSet.AffineTransPoint2d(hv_HomMat2DRotate, hv_cornerRow, hv_cornerCol,
                        out hv_newcornerRow, out hv_newcornerCol);
                    //gen_region_line (RegionLines1, newcornerRow, newcornerCol, circleInitRow, circleInitCol)
                    //stop ()
                    //第一个交点
                    hv_seekStart.Dispose();
                    hv_seekStart = 1.7;
                    hv_seekEnd.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_seekEnd = 3.14 + 1.7;
                    }

                    if ((int)((new HTuple(hv_index.TupleEqual(1))).TupleOr(new HTuple(hv_index.TupleEqual(
                        3)))) != 0)
                    {
                        hv_Row.Dispose(); hv_Column.Dispose();
                        HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                            hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                            hv_seekStart, hv_seekEnd, "positive", out hv_Row, out hv_Column);
                        //gen_cross_contour_xld (Cross, Row, Column, 60, 0.785398)
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            ho_Rectangle.Dispose();
                            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row.TupleSelect(0), hv_Column.TupleSelect(
                                0), ((hv_newRotate + 90)).TupleRad(), 12, 5);
                        }
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_MeasureHandle.Dispose();
                            HOperatorSet.GenMeasureRectangle2(hv_Row.TupleSelect(0), hv_Column.TupleSelect(
                                0), ((hv_newRotate + 90)).TupleRad(), 20, 5, hv_Width, hv_Height, "nearest_neighbor",
                                out hv_MeasureHandle);
                        }
                        hv_RowEdge.Dispose(); hv_ColumnEdge.Dispose(); hv_Amplitude.Dispose(); hv_Distance.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "positive",
                            "first", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                        ho_Cross1.Dispose();
                        HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_RowEdge, hv_ColumnEdge,
                            10, 0.785398);
                        HOperatorSet.CloseMeasure(hv_MeasureHandle);
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_RowEdge, out ExpTmpOutVar_0);
                            hv_cornerRowTuple.Dispose();
                            hv_cornerRowTuple = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_cornerColTuple, hv_ColumnEdge, out ExpTmpOutVar_0);
                            hv_cornerColTuple.Dispose();
                            hv_cornerColTuple = ExpTmpOutVar_0;
                        }
                        if ((int)((new HTuple(hv_RowEdge.TupleGreater(0))).TupleAnd(new HTuple(hv_isFindPoint.TupleEqual(
                            0)))) != 0)
                        {
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleConcat(hv_corner8Row, hv_RowEdge, out ExpTmpOutVar_0);
                                hv_corner8Row.Dispose();
                                hv_corner8Row = ExpTmpOutVar_0;
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleConcat(hv_corner8Col, hv_ColumnEdge, out ExpTmpOutVar_0);
                                hv_corner8Col.Dispose();
                                hv_corner8Col = ExpTmpOutVar_0;
                            }
                            hv_isFindPoint.Dispose();
                            hv_isFindPoint = 1;
                        }
                    }
                    //第二个交点
                    hv_seekStart1.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_seekStart1 = 3.14 + 1.7;
                    }
                    hv_seekEnd1.Dispose();
                    hv_seekEnd1 = 1.7;
                    if ((int)((new HTuple(hv_index.TupleEqual(2))).TupleOr(new HTuple(hv_index.TupleEqual(
                        4)))) != 0)
                    {
                        hv_Row1.Dispose(); hv_Column1.Dispose();
                        HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                            hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                            hv_seekStart1, hv_seekEnd1, "positive", out hv_Row1, out hv_Column1);
                        //gen_cross_contour_xld (Cross1, Row1, Column1, 60, 0.785398)
                        //gen_rectangle2 (Rectangle, Row1[0], Column1[0], rad(rotate+90), 12, 5)
                        using (HDevDisposeHelper dh = new HDevDisposeHelper())
                        {
                            hv_MeasureHandle.Dispose();
                            HOperatorSet.GenMeasureRectangle2(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                                0), ((hv_rotate + 90)).TupleRad(), 20, 5, hv_Width, hv_Height, "nearest_neighbor",
                                out hv_MeasureHandle);
                        }
                        hv_RowEdge1.Dispose(); hv_ColumnEdge1.Dispose(); hv_Amplitude1.Dispose(); hv_Distance1.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "negative",
                            "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                        ho_Cross2.Dispose();
                        HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_RowEdge1, hv_ColumnEdge1,
                            10, 0.785398);
                        HOperatorSet.CloseMeasure(hv_MeasureHandle);
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_cornerRowTuple1, hv_RowEdge1, out ExpTmpOutVar_0);
                            hv_cornerRowTuple1.Dispose();
                            hv_cornerRowTuple1 = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_cornerColTuple1, hv_ColumnEdge1, out ExpTmpOutVar_0);
                            hv_cornerColTuple1.Dispose();
                            hv_cornerColTuple1 = ExpTmpOutVar_0;
                        }
                        if ((int)((new HTuple(hv_RowEdge1.TupleGreater(0))).TupleAnd(new HTuple(hv_isFindPoint.TupleEqual(
                            0)))) != 0)
                        {
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleConcat(hv_corner8Row, hv_RowEdge1, out ExpTmpOutVar_0);
                                hv_corner8Row.Dispose();
                                hv_corner8Row = ExpTmpOutVar_0;
                            }
                            {
                                HTuple ExpTmpOutVar_0;
                                HOperatorSet.TupleConcat(hv_corner8Col, hv_ColumnEdge1, out ExpTmpOutVar_0);
                                hv_corner8Col.Dispose();
                                hv_corner8Col = ExpTmpOutVar_0;
                            }
                            hv_isFindPoint.Dispose();
                            hv_isFindPoint = 1;
                        }
                    }
                    // stop(...); only in hdevelop
                }
            }
            ho_Cross.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross, hv_corner8Row, hv_corner8Col, 100,
                0.785398);
            hv_Number.Dispose();
            HOperatorSet.CountObj(ho_Cross, out hv_Number);
            //stop ()
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_cornerRowTuple1, out ExpTmpOutVar_0);
                    hv_cornerRowTuple.Dispose();
                    hv_cornerRowTuple = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerColTuple, hv_cornerColTuple1, out ExpTmpOutVar_0);
                    hv_cornerColTuple.Dispose();
                    hv_cornerColTuple = ExpTmpOutVar_0;
                }
                if ((int)(new HTuple(hv_Number.TupleGreaterEqual(3))) != 0)
                {
                    ho_Contour.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_cornerRowTuple, hv_cornerColTuple);
                    hv_newCircleRow.Dispose(); hv_newCircleCol.Dispose(); hv_newCircleRadius.Dispose(); hv_StartPhi.Dispose(); hv_EndPhi.Dispose(); hv_PointOrder.Dispose();
                    HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2,
                        out hv_newCircleRow, out hv_newCircleCol, out hv_newCircleRadius, out hv_StartPhi,
                        out hv_EndPhi, out hv_PointOrder);
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_newCircleRow, hv_newCircleCol,
                        hv_newCircleRadius);
                }
                else
                {
                    hv_result.Dispose();
                    hv_result = 1;
                }
            }
            else
            {

            }
            ho_RegionLines.Dispose();
            ho_Rectangle.Dispose();
            ho_Cross1.Dispose();
            ho_Cross2.Dispose();
            ho_Contour.Dispose();
            ho_Circle1.Dispose();

            hv_Width.Dispose();
            hv_Height.Dispose();
            hv_cornerRowTuple.Dispose();
            hv_cornerColTuple.Dispose();
            hv_cornerRowTuple1.Dispose();
            hv_cornerColTuple1.Dispose();
            hv_corner8Row.Dispose();
            hv_corner8Col.Dispose();
            hv_index.Dispose();
            hv_rotate.Dispose();
            hv_isFindPoint.Dispose();
            hv_i.Dispose();
            hv_newRotate.Dispose();
            hv_HomMat2DIdentity.Dispose();
            hv_HomMat2DRotate.Dispose();
            hv_newcornerRow.Dispose();
            hv_newcornerCol.Dispose();
            hv_seekStart.Dispose();
            hv_seekEnd.Dispose();
            hv_Row.Dispose();
            hv_Column.Dispose();
            hv_MeasureHandle.Dispose();
            hv_RowEdge.Dispose();
            hv_ColumnEdge.Dispose();
            hv_Amplitude.Dispose();
            hv_Distance.Dispose();
            hv_seekStart1.Dispose();
            hv_seekEnd1.Dispose();
            hv_Row1.Dispose();
            hv_Column1.Dispose();
            hv_RowEdge1.Dispose();
            hv_ColumnEdge1.Dispose();
            hv_Amplitude1.Dispose();
            hv_Distance1.Dispose();
            hv_Number.Dispose();
            hv_StartPhi.Dispose();
            hv_EndPhi.Dispose();
            hv_PointOrder.Dispose();

            return;
        }
        public bool GetReviewPosition(HObject reviewLowImage, HObject reviewHighImage, FitCirclePara partFitCirclePara, FitLinePara partFitLine1Para, FitLinePara partFitLine2Para, FitCirclePara springFitCirclePara, FitLinePara springFitLine1Para, FitLinePara springFitLine2Para, 
            HObject puckCircleRegion, HObject puckLine1Region, HObject puckline2Region, HObject springCircleRegion, HObject springLine1Region, HObject springLine2Region, 
            double partOffCircleRadius, double springOffCircleRadius, double seekCornerStart, double seekCornerEnd,
   out HObject ho_fitPartCircleContour, out HObject ho_fitSpringCircleContour, out HObject ho_fitPartLine1Contour, out HObject ho_fitPartLine2Contour, out HObject ho_fitSpringLine1Contour, out HObject ho_fitSpringLine2Contour,
    out HObject ho_fitPartLine, out HObject ho_fitSpringLine, out HObject ho_circleDisLine, out double disOfTwoCircle, out double Offset_X, out double Offset_Y, out double offset_Angle, out string bugInfo)
        {
            bugInfo = "";
            Offset_X = 0;
            Offset_Y = 0;
            offset_Angle = 0;
            disOfTwoCircle = 0;
            HOperatorSet.GenEmptyObj(out ho_fitPartCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitSpringCircleContour);
            HOperatorSet.GenEmptyObj(out ho_fitPartLine1Contour);
            HOperatorSet.GenEmptyObj(out ho_fitPartLine2Contour);
            HOperatorSet.GenEmptyObj(out ho_fitSpringLine1Contour);
            HOperatorSet.GenEmptyObj(out ho_fitSpringLine2Contour);
            HOperatorSet.GenEmptyObj(out ho_fitPartLine);
            HOperatorSet.GenEmptyObj(out ho_fitSpringLine);
            HOperatorSet.GenEmptyObj(out ho_circleDisLine);
            try
            {
                //寻找Puck定位圆心...................................................
                #region Part定位圆心
                HTuple hv_partCircle_Row = null;
                HTuple hv_partCircle_Col = null;
                HTuple hv_partCircle_Radius = null;
                ho_fitPartCircleContour.Dispose();
                HTuple hv_circlePara = new HTuple();
                hv_circlePara[0] = partFitCirclePara.minGray;
                hv_circlePara[1] = partFitCirclePara.maxGray;
                hv_circlePara[2] = partFitCirclePara.contrast;
                hv_circlePara[3] = partFitCirclePara.fitNum;
                hv_circlePara[4] = partFitCirclePara.sigma;
                hv_circlePara[5] = partFitCirclePara.seekPointPath;
                FittingCircle(reviewLowImage, puckCircleRegion, out ho_fitPartCircleContour, hv_circlePara, out hv_partCircle_Row, out hv_partCircle_Col, out hv_partCircle_Radius);
                if (hv_partCircle_Radius <= 0)
                {
                    bugInfo = "寻找Part定位圆心失败！";
                    return false;
                }
                #endregion

                //寻找Spring定位圆心...................................................
                #region Spring定位圆心
                HTuple hv_springCircle_Row = null;
                HTuple hv_springCircle_Col = null;
                HTuple hv_springCircle_Radius = null;
                ho_fitSpringCircleContour.Dispose();
                HTuple hv_springCirclePara = new HTuple();
                hv_springCirclePara[0] = springFitCirclePara.minGray;
                hv_springCirclePara[1] = springFitCirclePara.maxGray;
                hv_springCirclePara[2] = springFitCirclePara.contrast;
                hv_springCirclePara[3] = springFitCirclePara.fitNum;
                hv_springCirclePara[4] = springFitCirclePara.sigma;
                hv_springCirclePara[5] = springFitCirclePara.seekPointPath;
                FittingCircle(reviewHighImage, springCircleRegion, out ho_fitSpringCircleContour, hv_springCirclePara, out hv_springCircle_Row, out hv_springCircle_Col, out hv_springCircle_Radius);
                if (hv_partCircle_Radius <= 0)
                {
                    bugInfo = "寻找Spring定位圆心失败！";
                    return false;
                }

                #endregion

                //寻找Puck线1....................................................
                #region Puck线1
                HTuple hv_partRowBeginLine1 = new HTuple(), hv_partColBeginLine1 = new HTuple();
                HTuple hv_partRowEndLine1 = new HTuple(), hv_partColEndLine1 = new HTuple();
                HTuple hv_Line1Para = new HTuple();
                hv_Line1Para[0] = partFitLine1Para.sigma;
                hv_Line1Para[1] = partFitLine1Para.contrast;
                switch (partFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_Line1Para[2] = "all";
                        break;
                    case 1:
                        hv_Line1Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line1Para[2] = "negative";
                        break;
                }
                switch (partFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_Line1Para[3] = "all";
                        break;
                    case 1:
                        hv_Line1Para[3] = "first";
                        break;
                    case 2:
                        hv_Line1Para[3] = "last";
                        break;
                }
                hv_Line1Para[4] = partFitLine1Para.fitNum;
                ho_fitPartLine1Contour.Dispose();
                FittingLine(reviewLowImage, puckLine1Region, out ho_fitPartLine1Contour, hv_Line1Para, partFitLine1Para.LSJudge,
                        partFitLine1Para.LSAngle, out hv_partRowBeginLine1, out hv_partColBeginLine1, out hv_partRowEndLine1, out hv_partColEndLine1);
                #endregion

                //寻找Puck线2....................................................
                #region Puck线2
                HTuple hv_partRowBeginLine2 = new HTuple(), hv_partColBeginLine2 = new HTuple();
                HTuple hv_partRowEndLine2 = new HTuple(), hv_partColEndLine2 = new HTuple();
                HTuple hv_Line2Para = new HTuple();
                hv_Line2Para[0] = partFitLine2Para.sigma;
                hv_Line2Para[1] = partFitLine2Para.contrast;
                switch (partFitLine2Para.grayTransition)
                {
                    case 0:
                        hv_Line2Para[2] = "all";
                        break;
                    case 1:
                        hv_Line2Para[2] = "positive";
                        break;
                    case 2:
                        hv_Line2Para[2] = "negative";
                        break;
                }
                switch (partFitLine2Para.pointPosition)
                {
                    case 0:
                        hv_Line2Para[3] = "all";
                        break;
                    case 1:
                        hv_Line2Para[3] = "first";
                        break;
                    case 2:
                        hv_Line2Para[3] = "last";
                        break;
                }
                hv_Line2Para[4] = partFitLine2Para.fitNum;
                ho_fitPartLine2Contour.Dispose();
                FittingLine(reviewLowImage, puckline2Region, out ho_fitPartLine2Contour, hv_Line2Para, partFitLine2Para.LSJudge,
                        partFitLine2Para.LSAngle, out hv_partRowBeginLine2, out hv_partColBeginLine2, out hv_partRowEndLine2, out hv_partColEndLine2);
                #endregion

                //寻找Spring线1....................................................
                #region Spring 线1
                HTuple hv_springRowBeginLine1 = new HTuple(), hv_springColBeginLine1 = new HTuple();
                HTuple hv_springRowEndLine1 = new HTuple(), hv_springColEndLine1 = new HTuple();
                HTuple hv_springLine1Para = new HTuple();
                hv_springLine1Para[0] = springFitLine1Para.sigma;
                hv_springLine1Para[1] = springFitLine1Para.contrast;
                switch (springFitLine1Para.grayTransition)
                {
                    case 0:
                        hv_springLine1Para[2] = "all";
                        break;
                    case 1:
                        hv_springLine1Para[2] = "positive";
                        break;
                    case 2:
                        hv_springLine1Para[2] = "negative";
                        break;
                }
                switch (springFitLine1Para.pointPosition)
                {
                    case 0:
                        hv_springLine1Para[3] = "all";
                        break;
                    case 1:
                        hv_springLine1Para[3] = "first";
                        break;
                    case 2:
                        hv_springLine1Para[3] = "last";
                        break;
                }
                hv_springLine1Para[4] = springFitLine1Para.fitNum;
                ho_fitSpringLine1Contour.Dispose();
                FittingLine(reviewHighImage, springLine1Region, out ho_fitSpringLine1Contour, hv_springLine1Para, springFitLine1Para.LSJudge,
                        springFitLine1Para.LSAngle, out hv_springRowBeginLine1, out hv_springColBeginLine1, out hv_springRowEndLine1, out hv_springColEndLine1);

                #endregion

                //寻找Spring线2....................................................
                #region Spring 线2
                HTuple hv_springRowBeginLine2 = new HTuple(), hv_springColBeginLine2 = new HTuple();
                HTuple hv_springRowEndLine2 = new HTuple(), hv_springColEndLine2 = new HTuple();
                HTuple hv_springLine2Para = new HTuple();
                hv_springLine2Para[0] = springFitLine2Para.sigma;
                hv_springLine2Para[1] = springFitLine2Para.contrast;
                switch (springFitLine2Para.grayTransition)
                {
                    case 0:
                        hv_springLine2Para[2] = "all";
                        break;
                    case 1:
                        hv_springLine2Para[2] = "positive";
                        break;
                    case 2:
                        hv_springLine2Para[2] = "negative";
                        break;
                }
                switch (springFitLine2Para.pointPosition)
                {
                    case 0:
                        hv_springLine2Para[3] = "all";
                        break;
                    case 1:
                        hv_springLine2Para[3] = "first";
                        break;
                    case 2:
                        hv_springLine2Para[3] = "last";
                        break;
                }
                hv_springLine2Para[4] = springFitLine2Para.fitNum;
                ho_fitSpringLine2Contour.Dispose();
                FittingLine(reviewHighImage, springLine2Region, out ho_fitSpringLine2Contour, hv_springLine2Para, springFitLine2Para.LSJudge,
                        springFitLine2Para.LSAngle, out hv_springRowBeginLine2, out hv_springColBeginLine2, out hv_springRowEndLine2, out hv_springColEndLine2);
                #endregion

                //求同心度
                HTuple hv_disOfTwoCircle = null;
                HOperatorSet.DistancePp(hv_partCircle_Row, hv_partCircle_Col, hv_springCircle_Row, hv_springCircle_Col, out hv_disOfTwoCircle);
                disOfTwoCircle = hv_disOfTwoCircle.D * 2;
                ho_circleDisLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_circleDisLine, hv_partCircle_Row, hv_partCircle_Col, hv_springCircle_Row, hv_springCircle_Col);
                //寻找Part两线和Part圆的交点....................................................
                HTuple partCorner1_row = null;
                HTuple partCorner1_col = null;
                HOperatorSet.IntersectionLineCircle(hv_partRowBeginLine1, hv_partColBeginLine1, hv_partRowEndLine1, hv_partColEndLine1, hv_partCircle_Row,
                    hv_partCircle_Col, partOffCircleRadius, seekCornerStart, seekCornerEnd, "positive", out partCorner1_row, out partCorner1_col);
                HTuple partCorner2_row = null;
                HTuple partCorner2_col = null;
                HOperatorSet.IntersectionLineCircle(hv_partRowBeginLine2, hv_partColBeginLine2, hv_partRowEndLine2, hv_partColEndLine2, hv_partCircle_Row,
                    hv_partCircle_Col, partOffCircleRadius, seekCornerStart, seekCornerEnd, "positive", out partCorner2_row, out partCorner2_col);
                //求part两个交点的中心与part圆心的连线.................................................
                HTuple partCornerCenter_row = (partCorner1_row + partCorner2_row) / 2;
                HTuple partCornerCenter_col = (partCorner1_col + partCorner2_col) / 2;
                if (partCornerCenter_row.Length == 0)
                {
                    bugInfo = "交点提取失败";
                    return false;
                }
                ho_fitPartLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_fitPartLine, hv_partCircle_Row, hv_partCircle_Col, partCornerCenter_row, partCornerCenter_col);

                //寻找Spring两线和spring圆的交点....................................................
                HTuple springCorner1_row = null;
                HTuple springCorner1_col = null;
                HOperatorSet.IntersectionLineCircle(hv_springRowBeginLine1, hv_springColBeginLine1, hv_springRowEndLine1, hv_springColEndLine1, hv_springCircle_Row,
                    hv_springCircle_Col, springOffCircleRadius, seekCornerStart, seekCornerEnd, "positive", out springCorner1_row, out springCorner1_col);
                HTuple springCorner2_row = null;
                HTuple springCorner2_col = null;
                HOperatorSet.IntersectionLineCircle(hv_springRowBeginLine2, hv_springColBeginLine2, hv_springRowEndLine2, hv_springColEndLine2, hv_springCircle_Row,
                    hv_springCircle_Col, springOffCircleRadius, seekCornerStart, seekCornerEnd, "positive", out springCorner2_row, out springCorner2_col);
                //求part两个交点的中心与part圆心的连线.................................................
                HTuple springCornerCenter_row = (springCorner1_row + springCorner2_row) / 2;
                HTuple springCornerCenter_col = (springCorner1_col + springCorner2_col) / 2;
                if (springCornerCenter_row.Length == 0)
                {
                    bugInfo = "交点提取失败";
                    return false;
                }
                if(false)
                {
                    HTuple newSpringCircle_Row, newSpringCircle_Col, newSpringCircle_radius = null;
                    Fit8PointsCircle(reviewHighImage, hv_springCircle_Row, hv_springCircle_Col, hv_springCircle_Radius, springCornerCenter_row, springCornerCenter_col, out newSpringCircle_Row, out newSpringCircle_Col, out newSpringCircle_radius);
                    hv_springCircle_Row = newSpringCircle_Row;
                    hv_springCircle_Col = newSpringCircle_Col;
                    hv_springCircle_Radius = newSpringCircle_radius;
                }
                ho_fitSpringLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_fitSpringLine, hv_partCircle_Row, hv_partCircle_Col, springCornerCenter_row, springCornerCenter_col);
                //求两条线的角度
                HTuple hv_angle = null;
                HOperatorSet.AngleLl(hv_partCircle_Row, hv_partCircle_Col, partCornerCenter_row, partCornerCenter_col, hv_springCircle_Row, hv_springCircle_Col, springCornerCenter_row, springCornerCenter_col, out hv_angle);
                //HOperatorSet.AngleLl(partCornerCenter_row, partCornerCenter_col, hv_partCircle_Row, hv_partCircle_Col, springCornerCenter_row, springCornerCenter_col, hv_partCircle_Row, hv_partCircle_Col, out hv_angle);
                offset_Angle = -hv_angle.D * 180 / Math.PI;
                //求偏移量
                HTuple hv_offset_X = null;
                HOperatorSet.DistancePl(hv_springCircle_Row, hv_springCircle_Col, hv_partCircle_Row, hv_partCircle_Col, partCornerCenter_row, partCornerCenter_col, out hv_offset_X);
                HTuple hv_pointPhi = null;
                HOperatorSet.AngleLl(hv_partCircle_Row, hv_partCircle_Col, partCornerCenter_row, partCornerCenter_col, hv_partCircle_Row, hv_partCircle_Col, hv_springCircle_Row, hv_springCircle_Col, out hv_pointPhi);
                Offset_X = hv_disOfTwoCircle.D * (hv_pointPhi.TupleSin());
                Offset_Y = -hv_disOfTwoCircle.D * (hv_pointPhi.TupleCos());
                if (hv_springCircle_Row <= hv_partCircle_Row)
                    Offset_X = -hv_offset_X.D;
                else
                    Offset_X = hv_offset_X.D;
                Offset_Y = Math.Sqrt(hv_disOfTwoCircle.D * hv_disOfTwoCircle.D - Offset_X * Offset_X);
                if (hv_springCircle_Col <= hv_partCircle_Col)
                    Offset_Y = -Offset_Y;
                //ho_Image.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                //ho_Image.Dispose();
                bugInfo = ex.Message;
                return false;
            }
        }
        public bool GetCoilReviewPosition(double coilReviewFitCircle1_X, double coilReviewFitCircle1_Y, double coidReviewLineCorner_X, double coidReviewLineCorner_Y,double coilReviewFitCircle2_X, double coilReviewFitCircle2_Y,
            double coilReviewFitLineS_X, double coilReviewFitLineS_Y, double coilReviewFitLineE_X, double coilReviewFitLineE_Y, out double disOfTwoCircle, out HObject ho_circleDisLine,
            out double offset_Angle, out double Offset_X, out double Offset_Y, out string bugInfo)
        {
            //out coidReviewLineCorner_X, out coidReviewLineCorner_Y, out angle
            //求同心度
            Offset_X = 0;
            Offset_Y = 0;
            offset_Angle = 0;
            disOfTwoCircle = 0;
            bugInfo = "";
             ho_circleDisLine = null;
            try
            {
                HTuple hv_disOfTwoCircle = null;
                HOperatorSet.DistancePp(coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coilReviewFitCircle2_X, coilReviewFitCircle2_Y, out hv_disOfTwoCircle);
                disOfTwoCircle = hv_disOfTwoCircle.D * 2;
                HOperatorSet.GenEmptyObj(out ho_circleDisLine);
                ho_circleDisLine.Dispose();
                HOperatorSet.GenRegionLine(out ho_circleDisLine, coilReviewFitCircle1_Y, coilReviewFitCircle1_X, coilReviewFitCircle2_Y, coilReviewFitCircle2_X);
                //求两条线的角度
                HTuple hv_angle = null;
                if (coilReviewFitLineS_X < coilReviewFitLineE_X)
                    HOperatorSet.AngleLl(coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coidReviewLineCorner_X, coidReviewLineCorner_Y,coilReviewFitLineS_X, coilReviewFitLineS_Y, coilReviewFitLineE_X, coilReviewFitLineE_Y,  out hv_angle);
                else
                    HOperatorSet.AngleLl(coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coidReviewLineCorner_X, coidReviewLineCorner_Y, coilReviewFitLineE_X, coilReviewFitLineE_Y, coilReviewFitLineS_X, coilReviewFitLineS_Y, out hv_angle);
                //HOperatorSet.AngleLl(partCornerCenter_row, partCornerCenter_col, hv_partCircle_Row, hv_partCircle_Col, springCornerCenter_row, springCornerCenter_col, hv_partCircle_Row, hv_partCircle_Col, out hv_angle);
                offset_Angle = -hv_angle.D * 180 / Math.PI - 0.02;
                //求偏移量
                //求偏移量
                //HTuple hv_offset_X = null;
                //HOperatorSet.DistancePl(coilReviewFitCircle2_X, coilReviewFitCircle2_Y, coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coidReviewLineCorner_X, coidReviewLineCorner_Y, out hv_offset_X);
                HTuple hv_pointPhi = null;
                HOperatorSet.AngleLl(coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coidReviewLineCorner_X, coidReviewLineCorner_Y, coilReviewFitCircle1_X, coilReviewFitCircle1_Y, coilReviewFitCircle2_X, coilReviewFitCircle2_Y, out hv_pointPhi);
                Offset_X = hv_disOfTwoCircle.D * (hv_pointPhi.TupleSin()) + 0.006;
                Offset_Y = -hv_disOfTwoCircle.D * (hv_pointPhi.TupleCos()) - 0.005;
                return true;
            }
            catch(Exception ex)
            {
                bugInfo = ex.Message;
                return false;
            }
        }

        public void Fit8PointsCircle(HObject ho_Image, HTuple hv_circleInitRow, HTuple hv_circleInitCol,
    HTuple hv_circleInitRadius, HTuple hv_cornerRow, HTuple hv_cornerCol, out HTuple hv_newCircleRow,
    out HTuple hv_newCircleCol, out HTuple hv_newCircleRadius)
        {




            // Local iconic variables 

            HObject ho_RegionLines, ho_RegionLines1 = null;
            HObject ho_Cross = null, ho_Rectangle = null, ho_Cross1 = null;
            HObject ho_Cross2 = null, ho_Contour = null, ho_Circle1 = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_cornerRowTuple = new HTuple(), hv_cornerColTuple = new HTuple();
            HTuple hv_cornerRowTuple1 = new HTuple(), hv_cornerColTuple1 = new HTuple();
            HTuple hv_index = new HTuple(), hv_rotate = new HTuple();
            HTuple hv_HomMat2DIdentity = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_newcornerRow = new HTuple(), hv_newcornerCol = new HTuple();
            HTuple hv_seekStart = new HTuple(), hv_seekEnd = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_Distance = new HTuple(), hv_seekStart1 = new HTuple();
            HTuple hv_seekEnd1 = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_RowEdge1 = new HTuple();
            HTuple hv_ColumnEdge1 = new HTuple(), hv_Amplitude1 = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_StartPhi = new HTuple();
            HTuple hv_EndPhi = new HTuple(), hv_PointOrder = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_Cross1);
            HOperatorSet.GenEmptyObj(out ho_Cross2);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            hv_newCircleRow = new HTuple();
            hv_newCircleCol = new HTuple();
            hv_newCircleRadius = new HTuple();
            //hv_newCircleRow.Dispose();
            hv_newCircleRow = 0;
            //hv_newCircleRadius.Dispose();
            hv_newCircleRadius = 0;
            //hv_newCircleCol.Dispose();
            hv_newCircleCol = 0;
            //gen_circle (Circle, circleInitRow, circleInitCol, circleInitRadius)
            //hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_RegionLines.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines, hv_circleInitRow, hv_circleInitCol,
                hv_cornerRow, hv_cornerCol);
            //hv_cornerRowTuple.Dispose();
            hv_cornerRowTuple = new HTuple();
            //hv_cornerColTuple.Dispose();
            hv_cornerColTuple = new HTuple();
            //hv_cornerRowTuple1.Dispose();
            hv_cornerRowTuple1 = new HTuple();
            //hv_cornerColTuple1.Dispose();
            hv_cornerColTuple1 = new HTuple();
            for (hv_index = 1; (int)hv_index <= 4; hv_index = (int)hv_index + 1)
            {
                //hv_rotate.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_rotate = 45 * hv_index;
                }
                //hv_HomMat2DIdentity.Dispose();
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    //hv_HomMat2DRotate.Dispose();
                    HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, hv_rotate.TupleRad(), hv_circleInitRow,
                        hv_circleInitCol, out hv_HomMat2DRotate);
                }
                //hv_newcornerRow.Dispose(); hv_newcornerCol.Dispose();
                HOperatorSet.AffineTransPoint2d(hv_HomMat2DRotate, hv_cornerRow, hv_cornerCol,
                    out hv_newcornerRow, out hv_newcornerCol);
                ho_RegionLines1.Dispose();
                HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_newcornerRow, hv_newcornerCol,
                    hv_circleInitRow, hv_circleInitCol);
                //第一个交点
                //hv_seekStart.Dispose();
                hv_seekStart = 0;
                //hv_seekEnd.Dispose();
                hv_seekEnd = 3.14;
                if ((int)(new HTuple(hv_index.TupleEqual(4))) != 0)
                {
                    //hv_seekStart.Dispose();
                    hv_seekStart = 1.57;
                    //hv_seekEnd.Dispose();
                    hv_seekEnd = 4.27;
                }
               // hv_Row.Dispose(); hv_Column.Dispose();
                HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                    hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                    hv_seekStart, hv_seekEnd, "positive", out hv_Row, out hv_Column);
                ho_Cross.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 20, 0.785398);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row.TupleSelect(0), hv_Column.TupleSelect(
                        0), hv_rotate.TupleRad(), 12, 5);
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    //hv_MeasureHandle.Dispose();
                    HOperatorSet.GenMeasureRectangle2(hv_Row.TupleSelect(0), hv_Column.TupleSelect(
                        0), hv_rotate.TupleRad(), 12, 5, hv_Width, hv_Height, "nearest_neighbor",
                        out hv_MeasureHandle);
                }
                //hv_RowEdge.Dispose(); hv_ColumnEdge.Dispose(); hv_Amplitude.Dispose(); hv_Distance.Dispose();
                HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 40, "positive", "all",
                    out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                ho_Cross1.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_RowEdge, hv_ColumnEdge, 20,
                    0.785398);
                HOperatorSet.CloseMeasure(hv_MeasureHandle);
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_RowEdge, out ExpTmpOutVar_0);
                    //hv_cornerRowTuple.Dispose();
                    hv_cornerRowTuple = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerColTuple, hv_ColumnEdge, out ExpTmpOutVar_0);
                    //hv_cornerColTuple.Dispose();
                    hv_cornerColTuple = ExpTmpOutVar_0;
                }
                //第二个交点
                //hv_seekStart1.Dispose();
                hv_seekStart1 = 3.14;
                //hv_seekEnd1.Dispose();
                hv_seekEnd1 = 6.28;
                if ((int)(new HTuple(hv_index.TupleEqual(4))) != 0)
                {
                    //hv_seekStart1.Dispose();
                    hv_seekStart1 = 4.27;
                    //hv_seekEnd1.Dispose();
                    hv_seekEnd1 = 1.57;
                }
                //hv_Row1.Dispose(); hv_Column1.Dispose();
                HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                    hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                    hv_seekStart1, hv_seekEnd1, "positive", out hv_Row1, out hv_Column1);
                ho_Cross1.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_Row1, hv_Column1, 20, 0.785398);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Rectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                        0), hv_rotate.TupleRad(), 12, 5);
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    //hv_MeasureHandle.Dispose();
                    HOperatorSet.GenMeasureRectangle2(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                        0), hv_rotate.TupleRad(), 12, 5, hv_Width, hv_Height, "nearest_neighbor",
                        out hv_MeasureHandle);
                }
                //hv_RowEdge1.Dispose(); hv_ColumnEdge1.Dispose(); hv_Amplitude1.Dispose(); hv_Distance1.Dispose();
                HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 40, "negative", "all",
                    out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                ho_Cross2.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_RowEdge1, hv_ColumnEdge1,
                    20, 0.785398);
                HOperatorSet.CloseMeasure(hv_MeasureHandle);
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerRowTuple1, hv_RowEdge1, out ExpTmpOutVar_0);
                    //hv_cornerRowTuple1.Dispose();
                    hv_cornerRowTuple1 = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerColTuple1, hv_ColumnEdge1, out ExpTmpOutVar_0);
                    //hv_cornerColTuple1.Dispose();
                    hv_cornerColTuple1 = ExpTmpOutVar_0;
                }
                //stop ()
            }
            if ((int)((new HTuple((new HTuple(hv_cornerRowTuple.TupleLength())).TupleGreater(
                0))).TupleAnd(new HTuple((new HTuple(hv_cornerRowTuple1.TupleLength())).TupleGreater(
                0)))) != 0)
            {
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_cornerRowTuple1, out ExpTmpOutVar_0);
                   // hv_cornerRowTuple.Dispose();
                    hv_cornerRowTuple = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerColTuple, hv_cornerColTuple1, out ExpTmpOutVar_0);
                    //hv_cornerColTuple.Dispose();
                    hv_cornerColTuple = ExpTmpOutVar_0;
                }
                ho_Contour.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_cornerRowTuple, hv_cornerColTuple);
                //hv_newCircleRow.Dispose(); hv_newCircleCol.Dispose(); hv_newCircleRadius.Dispose(); hv_StartPhi.Dispose(); hv_EndPhi.Dispose(); hv_PointOrder.Dispose();
                HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2, out hv_newCircleRow,
                    out hv_newCircleCol, out hv_newCircleRadius, out hv_StartPhi, out hv_EndPhi,
                    out hv_PointOrder);
                ho_Circle1.Dispose();
                HOperatorSet.GenCircle(out ho_Circle1, hv_newCircleRow, hv_newCircleCol, hv_newCircleRadius);
            }
            ho_RegionLines.Dispose();
            ho_RegionLines1.Dispose();
            ho_Cross.Dispose();
            ho_Rectangle.Dispose();
            ho_Cross1.Dispose();
            ho_Cross2.Dispose();
            ho_Contour.Dispose();
            ho_Circle1.Dispose();

            //hv_Width.Dispose();
            //hv_Height.Dispose();
            //hv_cornerRowTuple.Dispose();
            //hv_cornerColTuple.Dispose();
            //hv_cornerRowTuple1.Dispose();
            //hv_cornerColTuple1.Dispose();
            //hv_index.Dispose();
            //hv_rotate.Dispose();
            //hv_HomMat2DIdentity.Dispose();
            //hv_HomMat2DRotate.Dispose();
            //hv_newcornerRow.Dispose();
            //hv_newcornerCol.Dispose();
            //hv_seekStart.Dispose();
            //hv_seekEnd.Dispose();
            //hv_Row.Dispose();
            //hv_Column.Dispose();
            //hv_MeasureHandle.Dispose();
            //hv_RowEdge.Dispose();
            //hv_ColumnEdge.Dispose();
            //hv_Amplitude.Dispose();
            //hv_Distance.Dispose();
            //hv_seekStart1.Dispose();
            //hv_seekEnd1.Dispose();
            //hv_Row1.Dispose();
            //hv_Column1.Dispose();
            //hv_RowEdge1.Dispose();
            //hv_ColumnEdge1.Dispose();
            //hv_Amplitude1.Dispose();
            //hv_Distance1.Dispose();
            //hv_StartPhi.Dispose();
            //hv_EndPhi.Dispose();
            //hv_PointOrder.Dispose();

            return;
        }
        public void ho_AdaptiveImage(HWindowControl hv_showImageControl, int hv_ImageWidth, int hv_ImageHeight)
        {
            double rWindow = hv_showImageControl.Width * 1.0f / hv_showImageControl.Height;
            double rImage = hv_ImageWidth * 1.0f / hv_ImageHeight;
            if (rWindow > rImage)
            {
                double nw = (hv_ImageWidth * rWindow / rImage - hv_ImageWidth) / 2;
                double showWidth = nw + hv_ImageWidth;
                HOperatorSet.SetPart(hv_showImageControl.HalconWindow, 0, nw * (-1), hv_ImageHeight, showWidth);
            }
            else
            {
                double nh = (hv_showImageControl.Height - hv_showImageControl.Width / rImage) * hv_ImageHeight / hv_showImageControl.Height;
                double showHeight = nh + hv_ImageHeight;
                HOperatorSet.SetPart(hv_showImageControl.HalconWindow, nh * (-1), 0, showHeight, hv_ImageWidth);
            }
        }
        public void DefectDetect(HObject ho_Image, HObject ho_regionROI, out HObject ho_defectContour,
    HTuple hv_cfgPara, out HTuple hv_defectArea, out HTuple hv_defectRow, out HTuple hv_defectCol,
    out HTuple hv_defectMean, out HTuple hv_detectResult)
        {




            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];

            // Local iconic variables 

            HObject ho_defectUnion, ho_ImageReduced, ho_Region = null;
            HObject ho_ImageMean = null, ho_RegionClosing = null, ho_RegionOpening = null;
            HObject ho_ConnectedRegions, ho_SelectedRegions, ho_ObjectSelected = null;
            HObject ho_ConnectedRegions1 = null;

            // Local control variables 

            HTuple hv_detectType = null, hv_minGray = null;
            HTuple hv_maxGray = null, hv_mask = null, hv_stdDevScale = null;
            HTuple hv_absThreshold = null, hv_lightDark = null, hv_closRedius = null;
            HTuple hv_openRedius = null, hv_meanRadius = null, hv_minArea = null;
            HTuple hv_maxArea = null, hv_addArea = null, hv_minMean = null;
            HTuple hv_maxMean = null, hv_addMean = null, hv_minNum = null;
            HTuple hv_maxNum = null, hv_addNum = null, hv_areaFilter = null;
            HTuple hv_Number = null, hv_defectNum = null, hv_index = null;
            HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Mean = new HTuple(), hv_Deviation = new HTuple();
            HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
            HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple();
            HTuple hv_isFilterDown = new HTuple(), hv_isFilterNum = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_defectContour);
            HOperatorSet.GenEmptyObj(out ho_defectUnion);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            hv_detectResult = new HTuple();
            hv_detectType = hv_cfgPara[0];
            hv_minGray = hv_cfgPara[1];
            hv_maxGray = hv_cfgPara[2];
            hv_mask = hv_cfgPara[3];
            hv_stdDevScale = hv_cfgPara[4];
            hv_absThreshold = hv_cfgPara[5];
            hv_lightDark = hv_cfgPara[6];
            hv_closRedius = hv_cfgPara[7];
            hv_openRedius = hv_cfgPara[8];
            hv_meanRadius = hv_cfgPara[9];
            hv_minArea = hv_cfgPara[10];
            hv_maxArea = hv_cfgPara[11];
            hv_addArea = hv_cfgPara[12];
            hv_minMean = hv_cfgPara[13];
            hv_maxMean = hv_cfgPara[14];
            hv_addMean = hv_cfgPara[15];
            hv_minNum = hv_cfgPara[16];
            hv_maxNum = hv_cfgPara[17];
            hv_addNum = hv_cfgPara[18];
            hv_areaFilter = hv_cfgPara[19];
            //addArea=1代表不添加,0代表添加
            ho_defectContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_defectContour);
            ho_defectUnion.Dispose();
            HOperatorSet.GenEmptyObj(out ho_defectUnion);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_regionROI, out ho_ImageReduced);
            if ((int)(new HTuple(hv_detectType.TupleEqual(0))) != 0)
            {
                ho_Region.Dispose();
                HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_minGray, hv_maxGray);
            }
            else
            {
                ho_ImageMean.Dispose();
                HOperatorSet.MeanImage(ho_ImageReduced, out ho_ImageMean, hv_meanRadius, hv_meanRadius);
                ho_Region.Dispose();
                HOperatorSet.VarThreshold(ho_ImageMean, out ho_Region, hv_mask, hv_mask, hv_stdDevScale,
                    hv_absThreshold, hv_lightDark);
            }
            if ((int)(new HTuple(hv_closRedius.TupleGreater(0))) != 0)
            {
                //闭运算，缺陷连接系数
                ho_RegionClosing.Dispose();
                HOperatorSet.ClosingCircle(ho_Region, out ho_RegionClosing, hv_closRedius);
            }
            else
            {
                ho_RegionClosing.Dispose();
                ho_RegionClosing = ho_Region.CopyObj(1, -1);
            }
            if ((int)(new HTuple(hv_openRedius.TupleGreater(0))) != 0)
            {
                //开运算，缺陷断开系数
                ho_RegionOpening.Dispose();
                HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, hv_openRedius);
            }
            else
            {
                ho_RegionOpening.Dispose();
                ho_RegionOpening = ho_RegionClosing.CopyObj(1, -1);
            }
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions);
            //对缺陷的面积进行初次筛选
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
                "and", hv_areaFilter, 9999999);
            HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
            hv_defectRow = new HTuple();
            hv_defectCol = new HTuple();
            hv_defectArea = new HTuple();
            hv_defectMean = new HTuple();
            hv_defectNum = 0;
            HTuple end_val51 = hv_Number;
            HTuple step_val51 = 1;
            for (hv_index = 1; hv_index.Continue(end_val51, step_val51); hv_index = hv_index.TupleAdd(step_val51))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_SelectedRegions, out ho_ObjectSelected, hv_index);
                HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.Intensity(ho_ObjectSelected, ho_Image, out hv_Mean, out hv_Deviation);
                HOperatorSet.SmallestRectangle1(ho_ObjectSelected, out hv_Row1, out hv_Column1,
                    out hv_Row2, out hv_Column2);
                hv_isFilterDown = 0;
                //*首先对面积进行限定,0代表添加
                if ((int)(new HTuple(hv_addArea.TupleEqual(0))) != 0)
                {
                    //容许NG范围
                    if ((int)((new HTuple(hv_Area.TupleLess(hv_minArea))).TupleOr(new HTuple(hv_Area.TupleGreater(
                        hv_maxArea)))) != 0)
                    {
                        //不在NG范围，过滤掉该缺陷
                        hv_isFilterDown = 1;
                    }
                }
                //***对亮度进行限定
                if ((int)(new HTuple(hv_isFilterDown.TupleEqual(0))) != 0)
                {
                    if ((int)(new HTuple(hv_addMean.TupleEqual(0))) != 0)
                    {
                        //容许NG范围
                        if ((int)((new HTuple(hv_Mean.TupleLess(hv_minMean))).TupleOr(new HTuple(hv_Mean.TupleGreater(
                            hv_maxMean)))) != 0)
                        {
                            //不在NG范围，过滤掉该缺陷
                            hv_isFilterDown = 1;
                        }
                    }
                }
                //***没有过滤掉
                if ((int)(new HTuple(hv_isFilterDown.TupleEqual(0))) != 0)
                {
                    HOperatorSet.TupleConcat(hv_defectArea, hv_Area, out hv_defectArea);
                    HOperatorSet.TupleConcat(hv_defectRow, hv_Row, out hv_defectRow);
                    HOperatorSet.TupleConcat(hv_defectCol, hv_Column, out hv_defectCol);
                    HOperatorSet.TupleConcat(hv_defectMean, hv_Mean, out hv_defectMean);
                    {
                        HObject ExpTmpOutVar_0;
                        HOperatorSet.Union2(ho_ObjectSelected, ho_defectUnion, out ExpTmpOutVar_0
                            );
                        ho_defectUnion.Dispose();
                        ho_defectUnion = ExpTmpOutVar_0;
                    }
                    hv_defectNum = hv_defectNum + 1;
                }
            }
            //***通过对数量进行限定,通过数量判断是否过滤掉
            hv_isFilterNum = 0;
            if ((int)(new HTuple(hv_addNum.TupleEqual(0))) != 0)
            {
                //*添加数量限制
                //容许NG范围,若不在ng范围
                if ((int)((new HTuple(hv_defectNum.TupleLess(hv_minNum))).TupleOr(new HTuple(hv_defectNum.TupleGreater(
                    hv_maxNum)))) != 0)
                {
                    hv_isFilterNum = 1;
                }
            }
            if ((int)(new HTuple(hv_isFilterNum.TupleEqual(0))) != 0)
            {
                //*没有过滤掉
                ho_ConnectedRegions1.Dispose();
                HOperatorSet.Connection(ho_defectUnion, out ho_ConnectedRegions1);
                ho_defectContour.Dispose();
                ho_defectContour = ho_ConnectedRegions1.CopyObj(1, -1);
            }
            else
            {
                hv_defectRow = new HTuple();
                hv_defectCol = new HTuple();
                hv_defectArea = new HTuple();
                hv_defectMean = new HTuple();
            }
            //*判断检测结果，1为NG，0为OK
            if ((int)(new HTuple((new HTuple(hv_defectArea.TupleLength())).TupleGreater(0))) != 0)
            {
                hv_detectResult = 1;
            }
            else
            {
                hv_detectResult = 0;
            }
            ho_defectUnion.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
            ho_ImageMean.Dispose();
            ho_RegionClosing.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions.Dispose();
            ho_ObjectSelected.Dispose();
            ho_ConnectedRegions1.Dispose();

            return;
        }

        public void MeaCircleRingWidth(HObject ho_Image, HObject ho_ROI, HTuple hv_cfgPara,
    out HTuple hv_ringWidth, out HTuple hv_edgeRow1, out HTuple hv_edgeCol1, out HTuple hv_edgeRow2,
    out HTuple hv_edgeCol2)
        {




            // Local iconic variables 

            HObject ho_FitCircleContour, ho_ImageReduced;
            HObject ho_Region, ho_RegionOpening, ho_RegionDilation1;
            HObject ho_RegionTrans, ho_Rectangle = null, ho_RegionIntersection = null;
            HObject ho_meaRectangle = null, ho_RegionIntersection1 = null;

            // Local control variables 

            HTuple hv_minGray = null, hv_maxGray = null;
            HTuple hv_contrast = null, hv_fitPNum = null, hv_sigma = null;
            HTuple hv_Width = null, hv_Height = null, hv_Row1 = null;
            HTuple hv_Column1 = null, hv_Radius = null, hv_byNum = null;
            HTuple hv_i = null, hv_RowEnd = new HTuple(), hv_ColumnEnd = new HTuple();
            HTuple hv_RowCenter = new HTuple(), hv_ColCenter = new HTuple();
            HTuple hv_mRow = new HTuple(), hv_mColumn = new HTuple();
            HTuple hv_mPhi = new HTuple(), hv_mLength1 = new HTuple();
            HTuple hv_mLength2 = new HTuple(), hv_MaxLength = new HTuple();
            HTuple hv_meaArea = new HTuple(), hv_Row2 = new HTuple();
            HTuple hv_Column2 = new HTuple(), hv_interArea = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge1 = new HTuple();
            HTuple hv_ColumnEdge1 = new HTuple(), hv_Amplitude1 = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_RowEdge2 = new HTuple();
            HTuple hv_ColumnEdge2 = new HTuple(), hv_Amplitude2 = new HTuple();
            HTuple hv_Distance2 = new HTuple(), hv_Distance = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_FitCircleContour);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation1);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection);
            HOperatorSet.GenEmptyObj(out ho_meaRectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionIntersection1);
            hv_minGray = hv_cfgPara[0];
            hv_maxGray = hv_cfgPara[1];
            hv_contrast = hv_cfgPara[2];
            hv_fitPNum = hv_cfgPara[3];
            hv_sigma = hv_cfgPara[4];
            hv_ringWidth = new HTuple();
            hv_edgeRow1 = new HTuple();
            hv_edgeCol1 = new HTuple();
            hv_edgeRow2 = new HTuple();
            hv_edgeCol2 = new HTuple();
            ho_FitCircleContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_FitCircleContour);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI, out ho_ImageReduced);
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, hv_minGray, hv_maxGray);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningCircle(ho_Region, out ho_RegionOpening, 3.5);
            ho_RegionDilation1.Dispose();
            HOperatorSet.DilationCircle(ho_RegionOpening, out ho_RegionDilation1, 7);
            ho_RegionTrans.Dispose();
            HOperatorSet.ShapeTrans(ho_RegionDilation1, out ho_RegionTrans, "outer_circle");
            //dilation_circle (RegionTrans, RegionDilation, 7)
            HOperatorSet.SmallestCircle(ho_RegionTrans, out hv_Row1, out hv_Column1, out hv_Radius);
            HOperatorSet.TupleAbs(360 / hv_fitPNum, out hv_byNum);
            HTuple end_val20 = 360;
            HTuple step_val20 = hv_byNum;
            for (hv_i = 1; hv_i.Continue(end_val20, step_val20); hv_i = hv_i.TupleAdd(step_val20))
            {
                hv_RowEnd = hv_Row1 + ((((hv_i.TupleRad())).TupleSin()) * hv_Radius);
                hv_ColumnEnd = hv_Column1 + ((((hv_i.TupleRad())).TupleCos()) * hv_Radius);
                hv_RowCenter = (hv_RowEnd + hv_Row1) / 2;
                hv_ColCenter = (hv_ColumnEnd + hv_Column1) / 2;
                ho_Rectangle.Dispose();
                HOperatorSet.GenRectangle2(out ho_Rectangle, hv_RowCenter, hv_ColCenter, -(hv_i.TupleRad()
                    ), hv_Radius / 2, 5);
                ho_RegionIntersection.Dispose();
                HOperatorSet.Intersection(ho_Rectangle, ho_RegionDilation1, out ho_RegionIntersection
                    );
                HOperatorSet.SmallestRectangle2(ho_RegionIntersection, out hv_mRow, out hv_mColumn,
                    out hv_mPhi, out hv_mLength1, out hv_mLength2);
                if ((int)(new HTuple(hv_mLength1.TupleGreater(0))) != 0)
                {
                    HOperatorSet.TupleMax2(hv_mLength1, hv_mLength2, out hv_MaxLength);
                    ho_meaRectangle.Dispose();
                    HOperatorSet.GenRectangle2(out ho_meaRectangle, hv_mRow, hv_mColumn, -(hv_i.TupleRad()
                        ), hv_MaxLength, 5);
                    HOperatorSet.AreaCenter(ho_meaRectangle, out hv_meaArea, out hv_Row2, out hv_Column2);
                    ho_RegionIntersection1.Dispose();
                    HOperatorSet.Intersection(ho_meaRectangle, ho_ROI, out ho_RegionIntersection1
                        );
                    HOperatorSet.AreaCenter(ho_RegionIntersection1, out hv_interArea, out hv_Row,
                        out hv_Column);
                    //交集区域完全包含在ROI内
                    if ((int)(new HTuple(hv_interArea.TupleEqual(hv_meaArea))) != 0)
                    {
                        HOperatorSet.GenMeasureRectangle2(hv_mRow, hv_mColumn, -(hv_i.TupleRad()
                            ), hv_MaxLength, 5, hv_Width, hv_Height, "nearest_neighbor", out hv_MeasureHandle);
                        HOperatorSet.MeasurePos(ho_ImageReduced, hv_MeasureHandle, 1, hv_contrast,
                            "all", "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1,
                            out hv_Distance1);
                        HOperatorSet.MeasurePos(ho_ImageReduced, hv_MeasureHandle, 1, hv_contrast,
                            "all", "last", out hv_RowEdge2, out hv_ColumnEdge2, out hv_Amplitude2,
                            out hv_Distance2);
                        HOperatorSet.CloseMeasure(hv_MeasureHandle);
                        if ((int)((new HTuple(hv_RowEdge1.TupleGreater(0))).TupleAnd(new HTuple(hv_RowEdge2.TupleGreater(
                            0)))) != 0)
                        {
                            HOperatorSet.DistancePp(hv_RowEdge1, hv_ColumnEdge1, hv_RowEdge2, hv_ColumnEdge2,
                                out hv_Distance);
                            HOperatorSet.TupleConcat(hv_ringWidth, hv_Distance, out hv_ringWidth);
                            HOperatorSet.TupleConcat(hv_edgeRow1, hv_RowEdge1, out hv_edgeRow1);
                            HOperatorSet.TupleConcat(hv_edgeCol1, hv_ColumnEdge1, out hv_edgeCol1);
                            HOperatorSet.TupleConcat(hv_edgeRow2, hv_RowEdge2, out hv_edgeRow2);
                            HOperatorSet.TupleConcat(hv_edgeCol2, hv_ColumnEdge2, out hv_edgeCol2);
                        }
                        else
                        {
                            HOperatorSet.TupleConcat(hv_ringWidth, 0, out hv_ringWidth);
                            HOperatorSet.TupleConcat(hv_edgeRow1, 0, out hv_edgeRow1);
                            HOperatorSet.TupleConcat(hv_edgeCol1, 0, out hv_edgeCol1);
                            HOperatorSet.TupleConcat(hv_edgeRow2, 0, out hv_edgeRow2);
                            HOperatorSet.TupleConcat(hv_edgeCol2, 0, out hv_edgeCol2);
                        }
                    }
                }
            }
            //gen_region_line (RegionLines, edgeRow1, edgeCol1, edgeRow2, edgeCol2)
            //stop ()
            ho_FitCircleContour.Dispose();
            ho_ImageReduced.Dispose();
            ho_Region.Dispose();
            ho_RegionOpening.Dispose();
            ho_RegionDilation1.Dispose();
            ho_RegionTrans.Dispose();
            ho_Rectangle.Dispose();
            ho_RegionIntersection.Dispose();
            ho_meaRectangle.Dispose();
            ho_RegionIntersection1.Dispose();

            return;
        }

        public void JudgeTapeExist(HObject ho_Image, HTuple hv_matchModel, MatchPara locateMatchPara,out HTuple hv_result, out HTuple hv_meanGray, out HObject ho_SiMoROI)
        {

            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];

            // Local iconic variables 

            HObject  ho_TMP_Region;

            // Local control variables 

            HTuple hv_Mean = null, hv_Deviation = null;
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_SiMoROI);
            HOperatorSet.GenEmptyObj(out ho_TMP_Region);
            hv_result = 0;
            hv_meanGray = 0;
            ho_SiMoROI.Dispose();
            HOperatorSet.GenRectangle1(out ho_SiMoROI, ProjectInfo.Parameter.simoROI1R1, ProjectInfo.Parameter.simoROI1C1, ProjectInfo.Parameter.simoROI1R2, ProjectInfo.Parameter.simoROI1C2);
            ho_TMP_Region.Dispose();
            HOperatorSet.GenRectangle1(out ho_TMP_Region, ProjectInfo.Parameter.simoROI2R1, ProjectInfo.Parameter.simoROI2C1, ProjectInfo.Parameter.simoROI2R2, ProjectInfo.Parameter.simoROI2C2);
            {
                HObject ExpTmpOutVar_0;
                HOperatorSet.Union2(ho_SiMoROI, ho_TMP_Region, out ExpTmpOutVar_0);
                ho_SiMoROI.Dispose();
                ho_SiMoROI = ExpTmpOutVar_0;
            }

            //模板匹配.........................................................
            string bugInfo = "";
            int seekAngleStart = locateMatchPara.seekAngleStart;
            int seekAngleRange = locateMatchPara.seekAngleRange;
            int seekNum = locateMatchPara.seekModelNum;
            int seekScore = locateMatchPara.seekModelScore;
            HTuple hv_modelRow = new HTuple();
            HTuple hv_modelCol = new HTuple();
            HTuple hv_modelAngle = new HTuple();
            HTuple hv_modelScore = new HTuple();
            if (hv_matchModel == null)
            {
                bugInfo = "模板为空！";
            }
            if (locateMatchPara.modelMatchType == 0)  //基于形状
            {
                HOperatorSet.FindShapeModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01,
                        seekNum, 0.5, "least_squares", 0, 0.9, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore); //查找模板  
            }
            else if (locateMatchPara.modelMatchType == 1)  //基于灰度
            {
                HOperatorSet.FindNccModel(ho_Image, hv_matchModel, (new HTuple(seekAngleStart)).TupleRad(), (new HTuple(seekAngleRange)).TupleRad(), seekScore * 0.01, seekNum, 0.5, "true",
                    0, out hv_modelRow, out hv_modelCol, out hv_modelAngle, out hv_modelScore);
            }
            if (hv_modelScore.Length != 1)
            {
                bugInfo = "模板定位失败！";
            }
            //建立放射矩阵，并求除映射区域
            HTuple hv_HomMat2D;
            HOperatorSet.VectorAngleToRigid(locateMatchPara.modelLocationRow, locateMatchPara.modelLocationCol, 0, hv_modelRow[0],
                   hv_modelCol[0], hv_modelAngle[0], out hv_HomMat2D);   //根据模板坐标，求出放射矩阵
            HOperatorSet.AffineTransRegion(ho_SiMoROI, out ho_SiMoROI, hv_HomMat2D, "nearest_neighbor");
            HOperatorSet.Intensity(ho_SiMoROI, ho_Image, out hv_Mean, out hv_Deviation);
            hv_meanGray = hv_Mean.Clone();
            if ((int)(new HTuple(hv_Mean.TupleGreater(245))) != 0)
            {
                hv_result = 1;
            }
            ho_TMP_Region.Dispose();
            return;
        }
        public void GetCoilPuckLocateROI(HObject ho_Image, out HObject ho_ROI)
        {



            // Local iconic variables 

            HObject ho_ROI_0, ho_ImageReduced, ho_Regions;
            HObject ho_RegionOpening1, ho_RegionClosing, ho_ConnectedRegions;
            HObject ho_SelectedRegions1, ho_RegionFillUp1, ho_Circle1 = null;
            HObject ho_Circle2 = null;

            // Local control variables 

            HTuple hv_Number = new HTuple(), hv_Row = new HTuple();
            HTuple hv_Column = new HTuple(), hv_Radius = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ROI);
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening1);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp1);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            HOperatorSet.GenEmptyObj(out ho_Circle2);
            //gen_circle (ROI_0, 1496.58, 2032.77, 1433.21)
            ho_ROI_0.Dispose();
            HOperatorSet.GenCircle(out ho_ROI_0, 1498.33, 1924.46, 1448.91);

            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 0, 128);
            ho_RegionOpening1.Dispose();
            HOperatorSet.OpeningCircle(ho_Regions, out ho_RegionOpening1, 3.5);
            ho_RegionClosing.Dispose();
            HOperatorSet.ClosingCircle(ho_RegionOpening1, out ho_RegionClosing, 7.5);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionClosing, out ho_ConnectedRegions);
            ho_SelectedRegions1.Dispose();
            HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions1, "max_area",
                99);
            ho_RegionFillUp1.Dispose();
            HOperatorSet.FillUp(ho_SelectedRegions1, out ho_RegionFillUp1);

            //select_shape (ConnectedRegions, SelectedRegions, ['area','circularity'], 'and', [1000000,0.8], [99999999,1])
            //fill_up (SelectedRegions, RegionFillUp)
            //opening_circle (RegionFillUp1, RegionOpening, 10)
            hv_Number.Dispose();
            HOperatorSet.CountObj(ho_RegionFillUp1, out hv_Number);
            ho_ROI.Dispose();
            HOperatorSet.GenEmptyObj(out ho_ROI);
            if ((int)(new HTuple(hv_Number.TupleEqual(1))) != 0)
            {
                hv_Row.Dispose(); hv_Column.Dispose(); hv_Radius.Dispose();
                HOperatorSet.SmallestCircle(ho_RegionFillUp1, out hv_Row, out hv_Column, out hv_Radius);
                ho_Circle1.Dispose();
                HOperatorSet.GenCircle(out ho_Circle1, hv_Row, hv_Column, outerRadius);
                ho_Circle2.Dispose();
                HOperatorSet.GenCircle(out ho_Circle2, hv_Row, hv_Column, innerRadius);
                ho_ROI.Dispose();
                HOperatorSet.Difference(ho_Circle1, ho_Circle2, out ho_ROI);
            }
            ho_ROI_0.Dispose();
            ho_ImageReduced.Dispose();
            ho_Regions.Dispose();
            ho_RegionOpening1.Dispose();
            ho_RegionClosing.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions1.Dispose();
            ho_RegionFillUp1.Dispose();
            ho_Circle1.Dispose();
            ho_Circle2.Dispose();

            hv_Number.Dispose();
            hv_Row.Dispose();
            hv_Column.Dispose();
            hv_Radius.Dispose();

            return;
        }

        
        public void FitCoilLine(HObject ho_Image, out HObject ho_cornerCross, HTuple hv_puckCircle_row,
    HTuple hv_puckCircle_col, HTuple hv_puckCorner_row, HTuple hv_puckCorner_col,
    HTuple hv_lineRow_start, HTuple hv_lineCol_start, HTuple hv_lineRow_end, HTuple hv_lineCol_end,
    HTuple hv_offsetDis, out HTuple hv_fitLineRow_start, out HTuple hv_fitLineCol_start,
    out HTuple hv_fitLineRow_end, out HTuple hv_fitLineCol_end)
        {




            // Local iconic variables 

            HObject ho_RegionLines1, ho_Contour, ho_ParallelContours;
            HObject ho_ParallelContours1, ho_Cross1, ho_Cross2, ho_Contour1 = null;

            // Local control variables 

            HTuple hv_Width = null, hv_Height = null, hv_HomMat2DIdentity = null;
            HTuple hv_HomMat2DRotate = null, hv_newCornerRow = null;
            HTuple hv_newCornerCol = null, hv_corner_row1 = null, hv_corner_col1 = null;
            HTuple hv_IsOverlapping = null, hv_corner_row2 = null;
            HTuple hv_corner_col2 = null, hv_fitRows = null, hv_fitCols = null;
            HTuple hv_MetrologyHandle = null, hv_meaIndex1 = null;
            HTuple hv_meaIndex2 = null, hv_RectangleParameter1 = null;
            HTuple hv_RectangleParameter2 = null, hv_rowMean1 = new HTuple();
            HTuple hv_colMean1 = new HTuple(), hv_rowMean2 = new HTuple();
            HTuple hv_colMean2 = new HTuple(), hv_Nr = new HTuple();
            HTuple hv_Nc = new HTuple(), hv_Dist = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_cornerCross);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_ParallelContours);
            HOperatorSet.GenEmptyObj(out ho_ParallelContours1);
            HOperatorSet.GenEmptyObj(out ho_Cross1);
            HOperatorSet.GenEmptyObj(out ho_Cross2);
            HOperatorSet.GenEmptyObj(out ho_Contour1);
            hv_fitLineRow_start = 0;
            hv_fitLineCol_start = 0;
            hv_fitLineRow_end = 0;
            hv_fitLineCol_end = 0;
            ho_cornerCross.Dispose();
            HOperatorSet.GenEmptyObj(out ho_cornerCross);
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            //gen_region_line (RegionLines, centerRow, centerCol, cornerRow, cornerCol)
            HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
            HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, (new HTuple(-90)).TupleRad()
                , hv_puckCircle_row, hv_puckCircle_col, out hv_HomMat2DRotate);
            HOperatorSet.AffineTransPoint2d(hv_HomMat2DRotate, hv_puckCorner_row, hv_puckCorner_col,
                out hv_newCornerRow, out hv_newCornerCol);
            ho_RegionLines1.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines1, hv_newCornerRow, hv_newCornerCol,
                hv_puckCircle_row, hv_puckCircle_col);
            ho_Contour.Dispose();
            HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_puckCircle_row.TupleConcat(
                hv_newCornerRow), hv_puckCircle_col.TupleConcat(hv_newCornerCol));
            ho_ParallelContours.Dispose();
            HOperatorSet.GenParallelContourXld(ho_Contour, out ho_ParallelContours, "regression_normal",
                hv_offsetDis);
            ho_ParallelContours1.Dispose();
            HOperatorSet.GenParallelContourXld(ho_Contour, out ho_ParallelContours1, "regression_normal",
                -hv_offsetDis);

            HOperatorSet.IntersectionLineContourXld(ho_ParallelContours, hv_lineRow_start,
                hv_lineCol_start, hv_lineRow_end, hv_lineCol_end, out hv_corner_row1, out hv_corner_col1,
                out hv_IsOverlapping);
            HOperatorSet.IntersectionLineContourXld(ho_ParallelContours1, hv_lineRow_start,
                hv_lineCol_start, hv_lineRow_end, hv_lineCol_end, out hv_corner_row2, out hv_corner_col2,
                out hv_IsOverlapping);
            ho_Cross1.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_corner_row1, hv_corner_col1,
                100, 0.785398);
            ho_Cross2.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_corner_row2, hv_corner_col2,
                100, 0.785398);
            hv_fitRows = new HTuple();
            hv_fitCols = new HTuple();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_corner_row1,
                hv_corner_col1 - 20, hv_corner_row1, hv_corner_col1 + 20, 10, 2, 1, 30, (new HTuple("num_instances")).TupleConcat(
                "measure_transition"), (new HTuple(1)).TupleConcat("positive"), out hv_meaIndex1);
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_corner_row2,
                hv_corner_col2 - 20, hv_corner_row2, hv_corner_col2 + 20, 10, 2, 1, 30, (new HTuple("num_instances")).TupleConcat(
                "measure_transition"), (new HTuple(1)).TupleConcat("positive"), out hv_meaIndex2);
            HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_meaIndex1, "all",
                "result_type", "all_param", out hv_RectangleParameter1);
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_meaIndex2, "all",
                "result_type", "all_param", out hv_RectangleParameter2);
            if ((int)((new HTuple((new HTuple(hv_RectangleParameter1.TupleLength())).TupleEqual(
                4))).TupleAnd(new HTuple((new HTuple(hv_RectangleParameter2.TupleLength()
                )).TupleEqual(4)))) != 0)
            {
                HOperatorSet.TupleConcat(hv_fitRows, hv_RectangleParameter1.TupleSelect(0),
                    out hv_fitRows);
                HOperatorSet.TupleConcat(hv_fitRows, hv_RectangleParameter1.TupleSelect(2),
                    out hv_fitRows);
                HOperatorSet.TupleConcat(hv_fitRows, hv_RectangleParameter2.TupleSelect(0),
                    out hv_fitRows);
                HOperatorSet.TupleConcat(hv_fitRows, hv_RectangleParameter2.TupleSelect(2),
                    out hv_fitRows);

                HOperatorSet.TupleConcat(hv_fitCols, hv_RectangleParameter1.TupleSelect(1),
                    out hv_fitCols);
                HOperatorSet.TupleConcat(hv_fitCols, hv_RectangleParameter1.TupleSelect(3),
                    out hv_fitCols);
                HOperatorSet.TupleConcat(hv_fitCols, hv_RectangleParameter2.TupleSelect(1),
                    out hv_fitCols);
                HOperatorSet.TupleConcat(hv_fitCols, hv_RectangleParameter2.TupleSelect(3),
                    out hv_fitCols);

                hv_rowMean1 = ((hv_RectangleParameter1.TupleSelect(0)) + (hv_RectangleParameter1.TupleSelect(
                    2))) / 2;
                hv_colMean1 = ((hv_RectangleParameter1.TupleSelect(1)) + (hv_RectangleParameter1.TupleSelect(
                    3))) / 2;

                hv_rowMean2 = ((hv_RectangleParameter2.TupleSelect(0)) + (hv_RectangleParameter2.TupleSelect(
                    2))) / 2;
                hv_colMean2 = ((hv_RectangleParameter2.TupleSelect(1)) + (hv_RectangleParameter2.TupleSelect(
                    3))) / 2;

                ho_cornerCross.Dispose();
                HOperatorSet.GenCrossContourXld(out ho_cornerCross, hv_rowMean1.TupleConcat(
                    hv_rowMean2), hv_colMean1.TupleConcat(hv_colMean2), 50, 0.785398);
                ho_Contour1.Dispose();
                HOperatorSet.GenContourPolygonXld(out ho_Contour1, hv_fitRows, hv_fitCols);
                HOperatorSet.FitLineContourXld(ho_Contour1, "tukey", -1, 0, 5, 2, out hv_fitLineRow_start,
                    out hv_fitLineCol_start, out hv_fitLineRow_end, out hv_fitLineCol_end,
                    out hv_Nr, out hv_Nc, out hv_Dist);
                //stop ()
            }
            ho_RegionLines1.Dispose();
            ho_Contour.Dispose();
            ho_ParallelContours.Dispose();
            ho_ParallelContours1.Dispose();
            ho_Cross1.Dispose();
            ho_Cross2.Dispose();
            ho_Contour1.Dispose();

            return;
        }
        public void Fit8PointsCircle_puck(HObject ho_Image, out HObject ho_Cross, HTuple hv_circleInitRow,
            HTuple hv_circleInitCol, HTuple hv_circleInitRadius, HTuple hv_cornerRow, HTuple hv_cornerCol,
            out HTuple hv_newCircleRow, out HTuple hv_newCircleCol, out HTuple hv_newCircleRadius,
            out HTuple hv_result)
        {




            // Local iconic variables 

            HObject ho_RegionLines, ho_Cross1 = null, ho_Cross2 = null;
            HObject ho_Contour = null, ho_Circle1 = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_cornerRowTuple = new HTuple(), hv_cornerColTuple = new HTuple();
            HTuple hv_cornerRowTuple1 = new HTuple(), hv_cornerColTuple1 = new HTuple();
            HTuple hv_corner8Row = new HTuple(), hv_corner8Col = new HTuple();
            HTuple hv_index = new HTuple(), hv_rotate = new HTuple();
            HTuple hv_i = new HTuple(), hv_newRotate = new HTuple();
            HTuple hv_HomMat2DIdentity = new HTuple(), hv_HomMat2DRotate = new HTuple();
            HTuple hv_newcornerRow = new HTuple(), hv_newcornerCol = new HTuple();
            HTuple hv_seekStart = new HTuple(), hv_seekEnd = new HTuple();
            HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_MeasureHandle = new HTuple(), hv_RowEdge = new HTuple();
            HTuple hv_ColumnEdge = new HTuple(), hv_Amplitude = new HTuple();
            HTuple hv_Distance = new HTuple(), hv_seekStart1 = new HTuple();
            HTuple hv_seekEnd1 = new HTuple(), hv_Row1 = new HTuple();
            HTuple hv_Column1 = new HTuple(), hv_RowEdge1 = new HTuple();
            HTuple hv_ColumnEdge1 = new HTuple(), hv_Amplitude1 = new HTuple();
            HTuple hv_Distance1 = new HTuple(), hv_Number = new HTuple();
            HTuple hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple();
            HTuple hv_PointOrder = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_Cross1);
            HOperatorSet.GenEmptyObj(out ho_Cross2);
            HOperatorSet.GenEmptyObj(out ho_Contour);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            hv_newCircleRow = new HTuple();
            hv_newCircleCol = new HTuple();
            hv_newCircleRadius = new HTuple();
            hv_result = new HTuple();
            hv_result.Dispose();
            hv_result = 0;
            hv_newCircleRow.Dispose();
            hv_newCircleRow = 0;
            hv_newCircleRadius.Dispose();
            hv_newCircleRadius = 0;
            hv_newCircleCol.Dispose();
            hv_newCircleCol = 0;
            //gen_circle (Circle, circleInitRow, circleInitCol, circleInitRadius)
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_RegionLines.Dispose();
            HOperatorSet.GenRegionLine(out ho_RegionLines, hv_circleInitRow, hv_circleInitCol,
                hv_cornerRow, hv_cornerCol);
            hv_cornerRowTuple.Dispose();
            hv_cornerRowTuple = new HTuple();
            hv_cornerColTuple.Dispose();
            hv_cornerColTuple = new HTuple();
            hv_cornerRowTuple1.Dispose();
            hv_cornerRowTuple1 = new HTuple();
            hv_cornerColTuple1.Dispose();
            hv_cornerColTuple1 = new HTuple();
            hv_corner8Row.Dispose();
            hv_corner8Row = new HTuple();
            hv_corner8Col.Dispose();
            hv_corner8Col = new HTuple();
            for (hv_index = 1; (int)hv_index <= 4; hv_index = (int)hv_index + 1)
            {
                hv_rotate.Dispose();
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_rotate = 45 * hv_index;
                }
                for (hv_i = -5; (int)hv_i <= 5; hv_i = (int)hv_i + 1)
                {
                    hv_newRotate.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_newRotate = hv_rotate + (hv_i * 0.2);
                    }
                    hv_HomMat2DIdentity.Dispose();
                    HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_HomMat2DRotate.Dispose();
                        HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, hv_newRotate.TupleRad(),
                            hv_circleInitRow, hv_circleInitCol, out hv_HomMat2DRotate);
                    }
                    hv_newcornerRow.Dispose(); hv_newcornerCol.Dispose();
                    HOperatorSet.AffineTransPoint2d(hv_HomMat2DRotate, hv_cornerRow, hv_cornerCol,
                        out hv_newcornerRow, out hv_newcornerCol);
                    //gen_region_line (RegionLines1, newcornerRow, newcornerCol, circleInitRow, circleInitCol)
                    //stop ()
                    //第一个交点
                    hv_seekStart.Dispose();
                    hv_seekStart = 1.7;
                    hv_seekEnd.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_seekEnd = 3.14 + 1.7;
                    }
                    hv_Row.Dispose(); hv_Column.Dispose();
                    HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                        hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                        hv_seekStart, hv_seekEnd, "positive", out hv_Row, out hv_Column);
                    //gen_cross_contour_xld (Cross, Row, Column, 60, 0.785398)
                    //gen_rectangle2 (Rectangle, Row[0], Column[0], rad(newRotate), 10, 5)
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_MeasureHandle.Dispose();
                        HOperatorSet.GenMeasureRectangle2(hv_Row.TupleSelect(0), hv_Column.TupleSelect(
                            0), hv_newRotate.TupleRad(), 10, 5, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandle);
                    }
                    if ((int)(new HTuple(hv_index.TupleGreaterEqual(3))) != 0)
                    {
                        hv_RowEdge.Dispose(); hv_ColumnEdge.Dispose(); hv_Amplitude.Dispose(); hv_Distance.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "positive",
                            "first", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                    }
                    else
                    {
                        hv_RowEdge.Dispose(); hv_ColumnEdge.Dispose(); hv_Amplitude.Dispose(); hv_Distance.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "negative",
                            "first", out hv_RowEdge, out hv_ColumnEdge, out hv_Amplitude, out hv_Distance);
                    }

                    ho_Cross1.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_RowEdge, hv_ColumnEdge,
                        10, 0.785398);
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
                    {
                        HTuple ExpTmpOutVar_0;
                        HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_RowEdge, out ExpTmpOutVar_0);
                        hv_cornerRowTuple.Dispose();
                        hv_cornerRowTuple = ExpTmpOutVar_0;
                    }
                    {
                        HTuple ExpTmpOutVar_0;
                        HOperatorSet.TupleConcat(hv_cornerColTuple, hv_ColumnEdge, out ExpTmpOutVar_0);
                        hv_cornerColTuple.Dispose();
                        hv_cornerColTuple = ExpTmpOutVar_0;
                    }
                    if ((int)(new HTuple(hv_i.TupleEqual(0))) != 0)
                    {
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_corner8Row, hv_RowEdge, out ExpTmpOutVar_0);
                            hv_corner8Row.Dispose();
                            hv_corner8Row = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_corner8Col, hv_ColumnEdge, out ExpTmpOutVar_0);
                            hv_corner8Col.Dispose();
                            hv_corner8Col = ExpTmpOutVar_0;
                        }
                    }
                    //第二个交点
                    hv_seekStart1.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_seekStart1 = 3.14 + 1.7;
                    }
                    hv_seekEnd1.Dispose();
                    hv_seekEnd1 = 1.7;
                    hv_Row1.Dispose(); hv_Column1.Dispose();
                    HOperatorSet.IntersectionLineCircle(hv_newcornerRow, hv_newcornerCol, hv_circleInitRow,
                        hv_circleInitCol, hv_circleInitRow, hv_circleInitCol, hv_circleInitRadius,
                        hv_seekStart1, hv_seekEnd1, "positive", out hv_Row1, out hv_Column1);
                    //gen_cross_contour_xld (Cross1, Row1, Column1, 60, 0.785398)
                    //gen_rectangle2 (Rectangle, Row1[0], Column1[0], rad(rotate), 12, 5)
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_MeasureHandle.Dispose();
                        HOperatorSet.GenMeasureRectangle2(hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(
                            0), hv_rotate.TupleRad(), 12, 5, hv_Width, hv_Height, "nearest_neighbor",
                            out hv_MeasureHandle);
                    }
                    if ((int)(new HTuple(hv_index.TupleGreaterEqual(3))) != 0)
                    {
                        hv_RowEdge1.Dispose(); hv_ColumnEdge1.Dispose(); hv_Amplitude1.Dispose(); hv_Distance1.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "negative",
                            "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                    }
                    else
                    {
                        hv_RowEdge1.Dispose(); hv_ColumnEdge1.Dispose(); hv_Amplitude1.Dispose(); hv_Distance1.Dispose();
                        HOperatorSet.MeasurePos(ho_Image, hv_MeasureHandle, 1, 30, "positive",
                            "first", out hv_RowEdge1, out hv_ColumnEdge1, out hv_Amplitude1, out hv_Distance1);
                    }

                    ho_Cross2.Dispose();
                    HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_RowEdge1, hv_ColumnEdge1,
                        10, 0.785398);
                    HOperatorSet.CloseMeasure(hv_MeasureHandle);
                    {
                        HTuple ExpTmpOutVar_0;
                        HOperatorSet.TupleConcat(hv_cornerRowTuple1, hv_RowEdge1, out ExpTmpOutVar_0);
                        hv_cornerRowTuple1.Dispose();
                        hv_cornerRowTuple1 = ExpTmpOutVar_0;
                    }
                    {
                        HTuple ExpTmpOutVar_0;
                        HOperatorSet.TupleConcat(hv_cornerColTuple1, hv_ColumnEdge1, out ExpTmpOutVar_0);
                        hv_cornerColTuple1.Dispose();
                        hv_cornerColTuple1 = ExpTmpOutVar_0;
                    }
                    if ((int)(new HTuple(hv_i.TupleEqual(0))) != 0)
                    {
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_corner8Row, hv_RowEdge1, out ExpTmpOutVar_0);
                            hv_corner8Row.Dispose();
                            hv_corner8Row = ExpTmpOutVar_0;
                        }
                        {
                            HTuple ExpTmpOutVar_0;
                            HOperatorSet.TupleConcat(hv_corner8Col, hv_ColumnEdge1, out ExpTmpOutVar_0);
                            hv_corner8Col.Dispose();
                            hv_corner8Col = ExpTmpOutVar_0;
                        }
                    }
                    //        *endif
                    //stop ()
                }
            }
            ho_Cross.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross, hv_corner8Row, hv_corner8Col, 200,
                0.785398);
            hv_Number.Dispose();
            HOperatorSet.CountObj(ho_Cross, out hv_Number);
            //stop ()
            if ((int)(new HTuple(hv_Number.TupleGreater(0))) != 0)
            {
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerRowTuple, hv_cornerRowTuple1, out ExpTmpOutVar_0);
                    hv_cornerRowTuple.Dispose();
                    hv_cornerRowTuple = ExpTmpOutVar_0;
                }
                {
                    HTuple ExpTmpOutVar_0;
                    HOperatorSet.TupleConcat(hv_cornerColTuple, hv_cornerColTuple1, out ExpTmpOutVar_0);
                    hv_cornerColTuple.Dispose();
                    hv_cornerColTuple = ExpTmpOutVar_0;
                }
                if ((int)(new HTuple(hv_Number.TupleGreaterEqual(5))) != 0)
                {
                    ho_Contour.Dispose();
                    HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_cornerRowTuple, hv_cornerColTuple);
                    hv_newCircleRow.Dispose(); hv_newCircleCol.Dispose(); hv_newCircleRadius.Dispose(); hv_StartPhi.Dispose(); hv_EndPhi.Dispose(); hv_PointOrder.Dispose();
                    HOperatorSet.FitCircleContourXld(ho_Contour, "algebraic", -1, 0, 0, 3, 2,
                        out hv_newCircleRow, out hv_newCircleCol, out hv_newCircleRadius, out hv_StartPhi,
                        out hv_EndPhi, out hv_PointOrder);
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_newCircleRow, hv_newCircleCol,
                        hv_newCircleRadius);
                }
                else
                {
                    hv_result.Dispose();
                    hv_result = 1;
                }
            }
            else
            {
            }
            ho_RegionLines.Dispose();
            ho_Cross1.Dispose();
            ho_Cross2.Dispose();
            ho_Contour.Dispose();
            ho_Circle1.Dispose();

            hv_Width.Dispose();
            hv_Height.Dispose();
            hv_cornerRowTuple.Dispose();
            hv_cornerColTuple.Dispose();
            hv_cornerRowTuple1.Dispose();
            hv_cornerColTuple1.Dispose();
            hv_corner8Row.Dispose();
            hv_corner8Col.Dispose();
            hv_index.Dispose();
            hv_rotate.Dispose();
            hv_i.Dispose();
            hv_newRotate.Dispose();
            hv_HomMat2DIdentity.Dispose();
            hv_HomMat2DRotate.Dispose();
            hv_newcornerRow.Dispose();
            hv_newcornerCol.Dispose();
            hv_seekStart.Dispose();
            hv_seekEnd.Dispose();
            hv_Row.Dispose();
            hv_Column.Dispose();
            hv_MeasureHandle.Dispose();
            hv_RowEdge.Dispose();
            hv_ColumnEdge.Dispose();
            hv_Amplitude.Dispose();
            hv_Distance.Dispose();
            hv_seekStart1.Dispose();
            hv_seekEnd1.Dispose();
            hv_Row1.Dispose();
            hv_Column1.Dispose();
            hv_RowEdge1.Dispose();
            hv_ColumnEdge1.Dispose();
            hv_Amplitude1.Dispose();
            hv_Distance1.Dispose();
            hv_Number.Dispose();
            hv_StartPhi.Dispose();
            hv_EndPhi.Dispose();
            hv_PointOrder.Dispose();

            return;
        }

        public void GetGuideMoveCoor1(HObject ho_Image, out HObject ho_circleContour, out HTuple hv_coor_Row,
            out HTuple hv_coor_Col)
        {



            // Local iconic variables 

            HObject ho_ROI_0, ho_ImageReduced, ho_Regions;
            HObject ho_RegionClosing, ho_RegionOpening, ho_ConnectedRegions;
            HObject ho_SelectedRegions1,ho_SelectedRegions, ho_RegionUnion, ho_Circle1 = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_circleRow = new HTuple(), hv_circleCol = new HTuple();
            HTuple hv_Radius = new HTuple(), hv_MetrologyHandle = new HTuple();
            HTuple hv_meaIndex = new HTuple(), hv_circleParameter = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            hv_coor_Row = new HTuple();
            hv_coor_Col = new HTuple();
            ho_circleContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            hv_coor_Row.Dispose();
            hv_coor_Row = 0;
            hv_coor_Col.Dispose();
            hv_coor_Col = 0;
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ROI_0.Dispose();
            HOperatorSet.GenRectangle1(out ho_ROI_0, 54.0613, 502.12, 2949.02, 3958.89);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 3, 128);
            ho_RegionClosing.Dispose();
            HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 7.5);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, 3.5);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions);
            ho_SelectedRegions1.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, "area",
                "and", 20000, 9999999);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions, "row",
                "and", 300, hv_Width);
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion);
            hv_circleRow.Dispose(); hv_circleCol.Dispose(); hv_Radius.Dispose();
            HOperatorSet.SmallestCircle(ho_RegionUnion, out hv_circleRow, out hv_circleCol,
                out hv_Radius);
            if ((int)(new HTuple(hv_Radius.TupleGreater(900))) != 0)
            {
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_circleRow, hv_circleCol, hv_Radius - 3);
                }
                hv_MetrologyHandle.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                hv_meaIndex.Dispose();
                HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_circleRow,
                    hv_circleCol, hv_Radius-3, 200, 10, 1, 30, (new HTuple("num_instances")).TupleConcat(
                    "measure_transition"), (new HTuple(1)).TupleConcat("positive"), out hv_meaIndex);
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                hv_circleParameter.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_meaIndex, "all",
                    "result_type", "all_param", out hv_circleParameter);
                if ((int)(new HTuple((new HTuple(hv_circleParameter.TupleLength())).TupleEqual(
                    3))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        ho_circleContour.Dispose();
                        HOperatorSet.GenCircle(out ho_circleContour, hv_circleParameter.TupleSelect(
                            0), hv_circleParameter.TupleSelect(1), hv_circleParameter.TupleSelect(
                            2));
                    }
                    hv_coor_Row.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_coor_Row = hv_circleParameter.TupleSelect(
                            0);
                    }
                    hv_coor_Col.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_coor_Col = hv_circleParameter.TupleSelect(
                            1);
                    }
                }
            }
            ho_ROI_0.Dispose();
            ho_ImageReduced.Dispose();
            ho_Regions.Dispose();
            ho_RegionClosing.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            ho_RegionUnion.Dispose();
            ho_Circle1.Dispose();

            hv_Width.Dispose();
            hv_Height.Dispose();
            hv_circleRow.Dispose();
            hv_circleCol.Dispose();
            hv_Radius.Dispose();
            hv_MetrologyHandle.Dispose();
            hv_meaIndex.Dispose();
            hv_circleParameter.Dispose();

            return;
        }

        public void GetGuideMoveCoor(HObject ho_Image, out HObject ho_circleContour, out HTuple hv_coor_Row,
    out HTuple hv_coor_Col)
        {



            // Local iconic variables 

            HObject ho_ROI_0, ho_ImageReduced, ho_Regions;
            HObject ho_RegionClosing, ho_RegionOpening, ho_ConnectedRegions;
            HObject ho_SelectedRegions1, ho_SelectedRegions2, ho_SelectedRegions;
            HObject ho_RegionUnion, ho_Circle1 = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_circleRow = new HTuple(), hv_circleCol = new HTuple();
            HTuple hv_Radius = new HTuple(), hv_MetrologyHandle = new HTuple();
            HTuple hv_meaIndex = new HTuple(), hv_circleParameter = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_Circle1);
            hv_coor_Row = new HTuple();
            hv_coor_Col = new HTuple();
            ho_circleContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            hv_coor_Row.Dispose();
            hv_coor_Row = 0;
            hv_coor_Col.Dispose();
            hv_coor_Col = 0;
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ROI_0.Dispose();
            HOperatorSet.GenRectangle1(out ho_ROI_0, 54.0613, 502.12, 2949.02, 3958.89);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
            //threshold (ImageReduced, Regions, 3, 134)

            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 3, 128);
            ho_RegionClosing.Dispose();
            HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 7.5);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, 3.5);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions);
            ho_SelectedRegions1.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, "area",
                "and", 20000, 9999999);
            ho_SelectedRegions2.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions2, "row",
                "and", 300, hv_Width);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions2, out ho_SelectedRegions, "circularity",
                "and", 0.5, 1);
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion);
            hv_circleRow.Dispose(); hv_circleCol.Dispose(); hv_Radius.Dispose();
            HOperatorSet.SmallestCircle(ho_RegionUnion, out hv_circleRow, out hv_circleCol,
                out hv_Radius);
            if ((int)(new HTuple(hv_Radius.TupleGreater(900))) != 0)
            {
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_Circle1.Dispose();
                    HOperatorSet.GenCircle(out ho_Circle1, hv_circleRow, hv_circleCol, hv_Radius - 3);
                }
                hv_MetrologyHandle.Dispose();
                HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
                HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    hv_meaIndex.Dispose();
                    HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_circleRow,
                        hv_circleCol, hv_Radius - 3, 200, 10, 1, 30, (new HTuple("num_instances")).TupleConcat(
                        "measure_transition"), (new HTuple(1)).TupleConcat("positive"), out hv_meaIndex);
                }
                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
                hv_circleParameter.Dispose();
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_meaIndex, "all",
                    "result_type", "all_param", out hv_circleParameter);
                if ((int)(new HTuple((new HTuple(hv_circleParameter.TupleLength())).TupleEqual(
                    3))) != 0)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        ho_circleContour.Dispose();
                        HOperatorSet.GenCircle(out ho_circleContour, hv_circleParameter.TupleSelect(
                            0), hv_circleParameter.TupleSelect(1), hv_circleParameter.TupleSelect(
                            2));
                    }
                    hv_coor_Row.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_coor_Row = hv_circleParameter.TupleSelect(
                            0);
                    }
                    hv_coor_Col.Dispose();
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        hv_coor_Col = hv_circleParameter.TupleSelect(
                            1);
                    }
                }
            }
            ho_ROI_0.Dispose();
            ho_ImageReduced.Dispose();
            ho_Regions.Dispose();
            ho_RegionClosing.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions1.Dispose();
            ho_SelectedRegions2.Dispose();
            ho_SelectedRegions.Dispose();
            ho_RegionUnion.Dispose();
            ho_Circle1.Dispose();

            hv_Width.Dispose();
            hv_Height.Dispose();
            hv_circleRow.Dispose();
            hv_circleCol.Dispose();
            hv_Radius.Dispose();
            hv_MetrologyHandle.Dispose();
            hv_meaIndex.Dispose();
            hv_circleParameter.Dispose();

            return;
        }
        // Procedures 
        public void GetGuideMoveCoorFit(HObject ho_Image, out HObject ho_circleContour, out HObject ho_ROI_0,
            HTuple hv_cfgPara, out HTuple hv_coor_Row, out HTuple hv_coor_Col)
        {
            // Stack for temporary objects 
            HObject[] OTemp = new HObject[20];

            // Local iconic variables 

            HObject ho_ImageReduced, ho_Regions, ho_RegionClosing;
            HObject ho_RegionOpening, ho_ConnectedRegions, ho_SelectedRegions1;
            HObject ho_SelectedRegions, ho_RegionUnion, ho_TMP_Region = null;

            // Local control variables 

            HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
            HTuple hv_circleRow = new HTuple(), hv_circleCol = new HTuple();
            HTuple hv_Radius = new HTuple(), hv_circleRadius = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_TMP_Region);
            hv_coor_Row = new HTuple();
            hv_coor_Col = new HTuple();
            ho_circleContour.Dispose();
            HOperatorSet.GenEmptyObj(out ho_circleContour);
            hv_coor_Row.Dispose();
            hv_coor_Row = 0;
            hv_coor_Col.Dispose();
            hv_coor_Col = 0;
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            ho_ROI_0.Dispose();
            HOperatorSet.GenRectangle1(out ho_ROI_0, 54.0613, 502.12, 2949.02, 3958.89);
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions,3, 128);
            ho_RegionClosing.Dispose();
            HOperatorSet.ClosingCircle(ho_Regions, out ho_RegionClosing, 7.5);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningCircle(ho_RegionClosing, out ho_RegionOpening, 3.5);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions);
            ho_SelectedRegions1.Dispose();
            HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, "area",
                "and", 100000, 9999999);
            ho_SelectedRegions.Dispose();
            HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions, "row",
                "and", 500, hv_Width);
            HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions, "circularity",
    "and", 0.5, 1);
            HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions, "column",
"and", 1800, 2500);
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_SelectedRegions, out ho_RegionUnion);
            hv_circleRow.Dispose(); hv_circleCol.Dispose(); hv_Radius.Dispose();
            HOperatorSet.SmallestCircle(ho_RegionUnion, out hv_circleRow, out hv_circleCol,
                out hv_Radius);
            if ((int)(new HTuple(hv_Radius.TupleGreater(990))) != 0)
            {
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_ROI_0.Dispose();
                    HOperatorSet.GenCircle(out ho_ROI_0, hv_circleRow, hv_circleCol, hv_cfgPara.TupleSelect(
                        6));
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_TMP_Region.Dispose();
                    HOperatorSet.GenCircle(out ho_TMP_Region, hv_circleRow, hv_circleCol, hv_cfgPara.TupleSelect(
                        7));
                }
                {
                    HObject ExpTmpOutVar_0;
                    HOperatorSet.Difference(ho_ROI_0, ho_TMP_Region, out ExpTmpOutVar_0);
                    ho_ROI_0.Dispose();
                    ho_ROI_0 = ExpTmpOutVar_0;
                }

                ho_circleContour.Dispose(); hv_coor_Row.Dispose(); hv_coor_Col.Dispose(); hv_circleRadius.Dispose();
                FittingCircle(ho_Image, ho_ROI_0, out ho_circleContour, hv_cfgPara, out hv_coor_Row,
                    out hv_coor_Col, out hv_circleRadius);
                if (hv_circleRadius.D < 900 || hv_circleRadius.D > 1300 || hv_coor_Row > 1600 || hv_coor_Row<1300)
                {
                    hv_coor_Col = 0;
                    hv_coor_Row = 0;
                }
            }
            ho_ImageReduced.Dispose();
            ho_Regions.Dispose();
            ho_RegionClosing.Dispose();
            ho_RegionOpening.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_SelectedRegions1.Dispose();
            ho_SelectedRegions.Dispose();
            ho_RegionUnion.Dispose();
            ho_TMP_Region.Dispose();

            hv_Width.Dispose();
            hv_Height.Dispose();
            hv_circleRow.Dispose();
            hv_circleCol.Dispose();
            hv_Radius.Dispose();
            hv_circleRadius.Dispose();

            return;
        }

        public void FindCode(HObject ho_Image, out HTuple hv_code, out HTuple hv_result)
        {



            // Local iconic variables 

            HObject ho_ROI_0, ho_ImageReduced, ho_ImagePart;
            HObject ho_ImageMean, ho_ImageScaled, ho_SymbolXLDs;

            // Local control variables 

            HTuple hv_DataCodeHandle = new HTuple(), hv_ResultHandles = new HTuple();
            HTuple hv_ResultValues = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ImagePart);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
            hv_code = new HTuple();
            hv_result = new HTuple();
            hv_result.Dispose();
            hv_result = 0;
            ho_ROI_0.Dispose();
            HOperatorSet.GenRectangle1(out ho_ROI_0, 788.148, 298.156, 2217.2, 1972.2);

            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced);
            ho_ImagePart.Dispose();
            HOperatorSet.CropDomain(ho_ImageReduced, out ho_ImagePart);
            ho_ImageMean.Dispose();
            HOperatorSet.MeanImage(ho_ImagePart, out ho_ImageMean, 5, 5);
            ho_ImageScaled.Dispose();
            HOperatorSet.ScaleImage(ho_ImageMean, out ho_ImageScaled, 1.5, 0);
            hv_DataCodeHandle.Dispose();
            HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", new HTuple(), new HTuple(),
                out hv_DataCodeHandle);
            HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "persistence", 1);
            HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "default_parameters", "enhanced_recognition");
            HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "finder_pattern_tolerance",
                "high");
            ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_code.Dispose();
            HOperatorSet.FindDataCode2d(ho_ImageScaled, out ho_SymbolXLDs, hv_DataCodeHandle,
                new HTuple(), new HTuple(), out hv_ResultHandles, out hv_code);
            hv_ResultValues.Dispose();
            HOperatorSet.GetDataCode2dResults(hv_DataCodeHandle, "all_candidates", "status",
                out hv_ResultValues);

            if ((int)(new HTuple((new HTuple(hv_code.TupleLength())).TupleEqual(0))) != 0)
            {
                hv_result.Dispose();
                hv_result = 1;
            }
            ho_ROI_0.Dispose();
            ho_ImageReduced.Dispose();
            ho_ImagePart.Dispose();
            ho_ImageMean.Dispose();
            ho_ImageScaled.Dispose();
            ho_SymbolXLDs.Dispose();

            hv_DataCodeHandle.Dispose();
            hv_ResultHandles.Dispose();
            hv_ResultValues.Dispose();

            return;
        }

        public void DownCamCoorTrans(HTuple hv_pix_X, HTuple hv_pix_Y, out HTuple hv_robot_X,
            out HTuple hv_robot_Y)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_mpix_x = new HTuple(), hv_mpix_y = new HTuple();
            HTuple hv_mrobot_X = new HTuple(), hv_mrobot_Y = new HTuple();
            HTuple hv_HomMat2D = new HTuple();
            // Initialize local and output iconic variables 
            hv_robot_X = new HTuple();
            hv_robot_Y = new HTuple();
            //mpix_x := [1110.34,1095.25,3004.14,3041.10,1822.72]
            //mpix_y := [2303.47,778.3,777,2384.42,1632.78]
            //mrobot_X := [-165.812,-188.548,-188.546,-164.495,-175.57]
            //mrobot_Y := [567.723,567.724,539.021,539.021,557.254]
            hv_mpix_x.Dispose();
            hv_mpix_x = new HTuple();
            hv_mpix_x[0] = 2095.609;
            hv_mpix_x[1] = 3251.732;
            hv_mpix_x[2] = 3267.501;
            hv_mpix_x[3] = 2266.382;
            hv_mpix_x[4] = 1121.2;
            hv_mpix_x[5] = 1101.28;
            hv_mpix_x[6] = 1070.837;
            hv_mpix_x[7] = 2096.418;
            hv_mpix_x[8] = 3302.448;
            hv_mpix_y.Dispose();
            hv_mpix_y = new HTuple();
            hv_mpix_y[0] = 1606.689;
            hv_mpix_y[1] = 1599.971;
            hv_mpix_y[2] = 2586.119;
            hv_mpix_y[3] = 2588.634;
            hv_mpix_y[4] = 2597.856;
            hv_mpix_y[5] = 1673.681;
            hv_mpix_y[6] = 677.733;
            hv_mpix_y[7] = 682.547;
            hv_mpix_y[8] = 678.037;
            hv_mrobot_X.Dispose();
            hv_mrobot_X = new HTuple();
            hv_mrobot_X[0] = -156.299;
            hv_mrobot_X[1] = -156.304;
            hv_mrobot_X[2] = -141.304;
            hv_mrobot_X[3] = -141.304;
            hv_mrobot_X[4] = -141.305;
            hv_mrobot_X[5] = -155.302;
            hv_mrobot_X[6] = -170.303;
            hv_mrobot_X[7] = -170.303;
            hv_mrobot_X[8] = -170.302;
            hv_mrobot_Y.Dispose();
            hv_mrobot_Y = new HTuple();
            hv_mrobot_Y[0] = 560.404;
            hv_mrobot_Y[1] = 543.137;
            hv_mrobot_Y[2] = 543.137;
            hv_mrobot_Y[3] = 558.137;
            hv_mrobot_Y[4] = 575.137;
            hv_mrobot_Y[5] = 575.137;
            hv_mrobot_Y[6] = 575.137;
            hv_mrobot_Y[7] = 560.137;
            hv_mrobot_Y[8] = 542.138;

            hv_HomMat2D.Dispose();
            HOperatorSet.VectorToHomMat2d(hv_mpix_x, hv_mpix_y, hv_mrobot_X, hv_mrobot_Y,out hv_HomMat2D);
            hv_robot_X.Dispose(); hv_robot_Y.Dispose();
            HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_pix_X, hv_pix_Y, out hv_robot_X,out hv_robot_Y);

            hv_mpix_x.Dispose();
            hv_mpix_y.Dispose();
            hv_mrobot_X.Dispose();
            hv_mrobot_Y.Dispose();
            hv_HomMat2D.Dispose();

            return;
        }
        public void UpCam2CoorTrans(HTuple hv_pix_X, HTuple hv_pix_Y, out HTuple hv_robot_X,
             out HTuple hv_robot_Y)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_mpix_x = new HTuple(), hv_mpix_y = new HTuple();
            HTuple hv_mrobot_X = new HTuple(), hv_mrobot_Y = new HTuple();
            HTuple hv_HomMat2D = new HTuple();
            // Initialize local and output iconic variables 
            hv_robot_X = new HTuple();
            hv_robot_Y = new HTuple();
            hv_mpix_x.Dispose();
            hv_mpix_x = new HTuple();
            hv_mpix_x[0] = 3299.5;
            hv_mpix_x[1] = 1362.99;
            hv_mpix_x[2] = 2868.2;
            hv_mpix_x[3] = 2899.05;
            hv_mpix_x[4] = 1399.14;
            hv_mpix_y.Dispose();
            hv_mpix_y = new HTuple();
            hv_mpix_y[0] = 2330.5;
            hv_mpix_y[1] = 2264.24;
            hv_mpix_y[2] = 2282.61;
            hv_mpix_y[3] = 887.35;
            hv_mpix_y[4] = 528.03;
            hv_mrobot_X.Dispose();
            hv_mrobot_X = new HTuple();
            hv_mrobot_X[0] = -457.23;
            hv_mrobot_X[1] = -458.075;
            hv_mrobot_X[2] = -458.075;
            hv_mrobot_X[3] = -483.751;
            hv_mrobot_X[4] = -490.075;
            hv_mrobot_Y.Dispose();
            hv_mrobot_Y = new HTuple();
            hv_mrobot_Y[0] = 208.308;
            hv_mrobot_Y[1] = 172.303;
            hv_mrobot_Y[2] = 200.303;
            hv_mrobot_Y[3] = 200.307;
            hv_mrobot_Y[4] = 172.304;
            //mpix_x := [1344.75,1343.57,1344.40,1238.80,1451.5]
            //mpix_y := [468.67,577.33,686.12,467.17,471.5]
            //mrobot_X := [0,2,4,0,0]
            //mrobot_Y := [0,0,0,-2,2]
            hv_HomMat2D.Dispose();
            HOperatorSet.VectorToHomMat2d(hv_mpix_x, hv_mpix_y, hv_mrobot_X, hv_mrobot_Y,
                out hv_HomMat2D);
            hv_robot_X.Dispose(); hv_robot_Y.Dispose();
            HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_pix_X, hv_pix_Y, out hv_robot_X,
                out hv_robot_Y);

            hv_mpix_x.Dispose();
            hv_mpix_y.Dispose();
            hv_mrobot_X.Dispose();
            hv_mrobot_Y.Dispose();
            hv_HomMat2D.Dispose();

            return;
        }
        public void DownCamCoorTrans_old(HTuple hv_pix_X, HTuple hv_pix_Y, out HTuple hv_robot_X,
    out HTuple hv_robot_Y)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_mpix_x = new HTuple(), hv_mpix_y = new HTuple();
            HTuple hv_mrobot_X = new HTuple(), hv_mrobot_Y = new HTuple();
            HTuple hv_HomMat2D = new HTuple();
            // Initialize local and output iconic variables 
            hv_robot_X = new HTuple();
            hv_robot_Y = new HTuple();
            hv_mpix_x.Dispose();
            hv_mpix_x = new HTuple();
            hv_mpix_x[0] = 1110.34;
            hv_mpix_x[1] = 1095.25;
            hv_mpix_x[2] = 3004.14;
            hv_mpix_x[3] = 3041.10;
            hv_mpix_x[4] = 1822.72;
            hv_mpix_y.Dispose();
            hv_mpix_y = new HTuple();
            hv_mpix_y[0] = 2303.47;
            hv_mpix_y[1] = 778.3;
            hv_mpix_y[2] = 777;
            hv_mpix_y[3] = 2384.42;
            hv_mpix_y[4] = 1632.78;
            hv_mrobot_X.Dispose();
            hv_mrobot_X = new HTuple();
            hv_mrobot_X[0] = -165.812;
            hv_mrobot_X[1] = -188.548;
            hv_mrobot_X[2] = -188.546;
            hv_mrobot_X[3] = -164.495;
            hv_mrobot_X[4] = -175.57;
            hv_mrobot_Y.Dispose();
            hv_mrobot_Y = new HTuple();
            hv_mrobot_Y[0] = 567.723;
            hv_mrobot_Y[1] = 567.724;
            hv_mrobot_Y[2] = 539.021;
            hv_mrobot_Y[3] = 539.021;
            hv_mrobot_Y[4] = 557.254;
            //mpix_x := [2095.609,3251.732,3267.501,2266.382,1121.2,1101.28,1070.837,2096.418,3302.448]
            //mpix_y := [1606.689,1599.971,2586.119,2588.634,2597.856,1673.681,677.733,682.547,678.037]
            //mrobot_X := [-156.299,-156.304,-141.304,-141.304,-141.305,-155.302,-170.303,-170.303,-170.302]
            //mrobot_Y := [560.404,543.137,543.137,558.137,575.137,575.137,575.137,560.137,542.138]

            hv_HomMat2D.Dispose();
            HOperatorSet.VectorToHomMat2d(hv_mpix_x, hv_mpix_y, hv_mrobot_X, hv_mrobot_Y,out hv_HomMat2D);
            hv_robot_X.Dispose(); hv_robot_Y.Dispose();
            HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_pix_X, hv_pix_Y, out hv_robot_X,out hv_robot_Y);

            hv_mpix_x.Dispose();
            hv_mpix_y.Dispose();
            hv_mrobot_X.Dispose();
            hv_mrobot_Y.Dispose();
            hv_HomMat2D.Dispose();

            return;
        }

        public void UpCam2CoorTrans_old(HTuple hv_pix_X, HTuple hv_pix_Y, out HTuple hv_robot_X,
            out HTuple hv_robot_Y)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_mpix_x = new HTuple(), hv_mpix_y = new HTuple();
            HTuple hv_mrobot_X = new HTuple(), hv_mrobot_Y = new HTuple();
            HTuple hv_HomMat2D = new HTuple();
            // Initialize local and output iconic variables 
            hv_robot_X = new HTuple();
            hv_robot_Y = new HTuple();
            //mpix_x := [3299.5,1362.99,2868.2,2899.05,1399.14]
            //mpix_y := [2330.5,2264.24,2282.61,887.35,528.03]
            //mrobot_X := [-457.23,-458.075,-458.075,-483.751,-490.075]
            //mrobot_Y := [208.308,172.303,200.303,200.307,172.304]
            hv_mpix_x.Dispose();
            hv_mpix_x = new HTuple();
            hv_mpix_x[0] = 1344.75;
            hv_mpix_x[1] = 1343.57;
            hv_mpix_x[2] = 1344.40;
            hv_mpix_x[3] = 1238.80;
            hv_mpix_x[4] = 1451.5;
            hv_mpix_y.Dispose();
            hv_mpix_y = new HTuple();
            hv_mpix_y[0] = 468.67;
            hv_mpix_y[1] = 577.33;
            hv_mpix_y[2] = 686.12;
            hv_mpix_y[3] = 467.17;
            hv_mpix_y[4] = 471.5;
            hv_mrobot_X.Dispose();
            hv_mrobot_X = new HTuple();
            hv_mrobot_X[0] = 0;
            hv_mrobot_X[1] = 2;
            hv_mrobot_X[2] = 4;
            hv_mrobot_X[3] = 0;
            hv_mrobot_X[4] = 0;
            hv_mrobot_Y.Dispose();
            hv_mrobot_Y = new HTuple();
            hv_mrobot_Y[0] = 0;
            hv_mrobot_Y[1] = 0;
            hv_mrobot_Y[2] = 0;
            hv_mrobot_Y[3] = -2;
            hv_mrobot_Y[4] = 2;
            hv_HomMat2D.Dispose();
            HOperatorSet.VectorToHomMat2d(hv_mpix_x, hv_mpix_y, hv_mrobot_X, hv_mrobot_Y,
                out hv_HomMat2D);
            hv_robot_X.Dispose(); hv_robot_Y.Dispose();
            HOperatorSet.AffineTransPoint2d(hv_HomMat2D, hv_pix_X, hv_pix_Y, out hv_robot_X,
                out hv_robot_Y);

            hv_mpix_x.Dispose();
            hv_mpix_y.Dispose();
            hv_mrobot_X.Dispose();
            hv_mrobot_Y.Dispose();
            hv_HomMat2D.Dispose();

            return;
        }


        #region Bmp与HObject之间的互转
        /// <summary>
        /// 彩色bmp转HObject
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public HObject RGBitmap2HObject(Bitmap bmp)
        {
            try
            {
                if (bmp.PixelFormat != PixelFormat.Format24bppRgb)
                { }
                int width = bmp.Width;
                int height = bmp.Height;
                Rectangle rect = new Rectangle(0, 0, width, height);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                int stride = bmpData.Stride;
                byte[] dataBlue = new byte[width * height];
                byte[] dataRed = new byte[width * height];
                byte[] dataGreen = new byte[width * height];
                //  HObject hobject;
                unsafe
                {
                    byte* ptr = (byte*)bmpData.Scan0;
                    fixed (byte* ptr_b = dataBlue, ptr_g = dataGreen, ptr_r = dataRed)
                    {

                        byte* pb = ptr_b;
                        byte* pg = ptr_g;
                        byte* pr = ptr_r;

                        for (int row = 0; row < height; row++)
                        {
                            for (int col = 0; col < width; col++)
                            {
                                pb[0] = ptr[0];
                                pg[0] = ptr[1];
                                pr[0] = ptr[2];
                                ptr += 3;
                                pb++;
                                pg++;
                                pr++;
                            }
                            ptr += stride - 3 * width;
                        }

                        HObject hobject;
                        HOperatorSet.GenImage3(out hobject, "byte", bmp.Width, bmp.Height, (int)ptr_r, (int)ptr_g, (int)ptr_b);
                        bmp.UnlockBits(bmpData);
                        return hobject;
                    }
                }
            }
            catch (Exception e)
            {
                bmp.Dispose();
                return null;
            }
            finally
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }
        /// <summary>
        /// 黑白bmp转HObject
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public HObject Bitmap2HObject(Bitmap bmp)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                IntPtr pointer = bmpData.Scan0;
                HObject ho_image;
                HOperatorSet.GenEmptyObj(out ho_image);
                ho_image.Dispose();
                HOperatorSet.GenImage1(out ho_image, "byte", bmp.Width, bmp.Height, pointer);
                bmp.UnlockBits(bmpData);
                return ho_image;
            }
            catch (Exception e)
            {
                bmp.Dispose();
                return null;
            }
        }
        public HObject BmpToHobject(Bitmap bmp)
        {
            HObject hv_image = null;
            HOperatorSet.GenEmptyObj(out hv_image);
            hv_image.Dispose();
            if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)  //判断格式为灰度图像,Bitmap转HObject
            {
                hv_image = Bitmap2HObject(bmp);
            }
            else if (bmp.PixelFormat == PixelFormat.Format24bppRgb || bmp.PixelFormat == PixelFormat.Format32bppRgb)  //彩色图像
            {
                hv_image = RGBitmap2HObject(bmp);
            }
            return hv_image;
        }


        #endregion
    }
}
